首页 > 其他分享 >WPF Binding中的RelativeSource属性

WPF Binding中的RelativeSource属性

时间:2024-10-09 18:12:16浏览次数:1  
标签:控件 对象 Binding RelativeSource Grid WPF 模板

一、简介

一个在Binding中比较重要的知识点——RelativeSource. 使用RelativeSource对象指向源对象。用这个可以在当前元素的基础上查找其他对象用于绑定到源对象。
在实际使用Binding的过程中大部分时间Binding都放在了数据模板和控件模板中,(数据模板是控件模板用于定义控件的UI)。
在模板中编写Binding时有时候无法直接拿到我们需要绑定的数据对象,我们不能确定我们需要的Source对象叫什么,但是我们直到了我们需要使用的对象在UI布局上的相对关系。比如控件自己关联了某个数据,关键自己某个层级的容器数据。这个时候我们的RelativeSource就派上了用场。我们使用RelativeSource首先要3个关键参数。
AncestorType=我们需要查找的类型。比如Grid
AncestorLevel= 我们需要向上查找几级。
Path=我们找到的元素需要绑定的属性。

二、代码

<!--嵌套Grid-->
    <Grid x:Name="G0" Margin="12" Background="Red">
        <TextBlock Text="In this Grid0 container"/>
        <Grid x:Name="G1" Margin="12" Background="Blue">
            <TextBlock Text="In this Grid1 container"/>
            <Grid x:Name="G2" Margin="12" Background="Yellow">
                <TextBlock Text="In this Grid2 container"/>
                <Grid x:Name="G3" Margin="12" Background="Beige">
                    <StackPanel>
                        <TextBlock Text="In this Grid3 container"/>
                        <!--AncestorType=我们需要查找的类型。比如Grid-->
                        <!--AncestorLevel= 我们需要向上查找几级-->
                        <!--Path=我们找到的元素需要绑定的属性。-->
                        <TextBlock Name="ces" Text="{Binding RelativeSource={RelativeSource AncestorType=Grid,AncestorLevel=1},Path=Name}"/>
                    </StackPanel>
                </Grid>
            </Grid>
        </Grid>
    </Grid>

三、运行结果

我们嵌套几个Grid,并在每个嵌套的Grid中都放入了一行文本用来显示自己所在的位置。设置了Margin使他有部分的重叠,可以更好的看到相互之间的层级关系。最内层使用一个TextBlock.在TextBlock的Text属性上使用RelativeSource。通过修改AncestorLevel 来设置向上查找Grid的等级。我们设置为1.向外层查找第一个找到的Grid对象。并绑定对应的Name。

 

 

标签:控件,对象,Binding,RelativeSource,Grid,WPF,模板
From: https://www.cnblogs.com/Peretsoft/p/18454821

相关文章

  • WPF中的ListBox怎么添加删除按钮并删除所在行
    直接上代码:第一步:创建测试类publicclassBeautifulGirl{publicstringName{get;set;}}第二步:创建viewmodel和数据源publicclassMainWindowVm{publicObservableCollection<BeautifulGirl>Girls{get;set;}......
  • Error:WPF项目中使用oxyplot,错误提示命名空间中不存在“Plot”名称
    在OxyPlot中,<oxy:PlotView>和<oxy:Plot>都是用来显示图表的控件,在WPF项目中使用oxyplot之前,先通过NuGet安装依赖包:OxyPlot.Wpf。<oxy:PlotView>和<oxy:Plot>使用示例:<oxy:PlotView>控件是一个视图控件,它绑定到一个PlotModel对象。这意味着你可以创建一个PlotModel实例,配置......
  • WPF MVVM第一篇-MVVM框架搭建
    1.创建view界面<Windowx:Class="WpfFramework.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schema......
  • WPF string format
    Text="{BindingStringFormat={}{0}items,Source={StaticResourcemainVM},Path=Cnt}"                //xaml<Windowx:Class="WpfApp17.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/pre......
  • Databinding(kotlin)
    简单使用(只作为view获取)build.gradle.kts配置android{dataBinding{enable=true}}activity注入//setContentView(R.layout.activity_main)valbinding:ActivityMainBinding=DataBindingUtil.setContentView(this,R.layout.activity_main......
  • WPF ListBoxItem Selected and background changed at the same time via ItemContain
    <Window.Resources><Stylex:Key="lbxItemContainerStyle"TargetType="ListBoxItem"><SetterProperty="Template"><Setter.Value><ControlTemplateTargetType=&quo......
  • WPF ListBox IsSynchronizedWithCurrentItem True ScrollIntoView via behavior CallM
    <ListBoxGrid.Column="0"ItemContainerStyle="{StaticResourcelbxItemContainerStyle}"ItemsSource="{BindingBooksCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"IsSynchronizedWith......
  • WPF ListBox IsSynchronizedWithCurrentItem="True"
    //xaml<Windowx:Class="WpfApp13.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mic......
  • WPF VirtualizingPanel.CacheLength="1" VirtualizingPanel.CacheLengthUnit="Item" c
    <ListBoxItemsSource="{BindingBooksCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"SelectedIndex="{BindingImgIdx,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"VirtualizingPanel.IsVirtualizing=......
  • WPF 闪烁动画
    实现使用一个Boder控件,通过后台属性控制Boder的背景色变化,实现闪烁的效果。1.xaml视图代码<BorderWidth="15"Height="15"Margin="25000"><Border.Style><StyleTargetType="{x:TypeBorder}"><SetterPro......