WPF中的binding(九)- 使用Binding的RelativeSource

我们进行Bingding时,如果明确知道数据源的Name,就能用Source或者ElementName进行绑定,但是有时候我们需要绑定的数据源可能没有明确的Name,此时我们就需要利用Bingding的RelativeSource进行绑定。

一、控件关联自身的属性

<Window x:Class="_6_27.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid x:Name="g1" Background="Red" Margin="10">
        <DockPanel Name="d1" Background="Orange" Margin="10">
            <Grid x:Name="g2" Background="Yellow" Margin="10">
                <DockPanel Name="d2" Background="LawnGreen" Margin="10">
                    <TextBox Name="textbox" FontSize="24" Margin="10"
                             Text="{Binding RelativeSource={RelativeSource Mode=Self},Path=Name}"/>
                </DockPanel>
            </Grid>
        </DockPanel>
    </Grid>
</Window>


关联控件本身的属性时,只需要设置RelativeSource的Mode=Self即可。


其等效的cs代码:

RelativeSource rs = new RelativeSource();
 rs.Mode = RelativeSourceMode.Self;
 Binding binding = new Binding("Name") { RelativeSource = rs };
 this.textbox.SetBinding(TextBox.TextProperty, binding);

运行效果如下:


二、控件关联其父级容器的属性

<Window x:Class="_6_27.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid x:Name="g1" Background="Red" Margin="10">
        <DockPanel Name="d1" Background="Orange" Margin="10">
            <Grid x:Name="g2" Background="Yellow" Margin="10">
                <DockPanel Name="d2" Background="LawnGreen" Margin="10">
                    <TextBox Name="textbox" FontSize="24" Margin="10"
                             Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Grid},AncestorLevel=1},Path=Name}"/>
                </DockPanel>
            </Grid>
        </DockPanel>
    </Grid>
</Window>

RelativeSource属性的数据类型为RelativeSource类,AncestorLevel指的是以Bingding目标控件为起点的层级偏移量,d2的偏移量是1,g2的偏移量是2,AncestorType指的是要找的目标对象的类型。值得注意的是AncestorLevel需要参考AncestorType使用,如上面设置了AncestorType={x:Type Grid},则Bingding在寻找时会忽略非Grid的控件,此时g2的偏移量是1,g1的偏移量是2,DockPanel被忽略。

你可以使用以下步骤将 ComboBox 的 SelectionChanged 事件绑定到 ViewModel 的一个命令: 1. 在 ViewModel 定义一个 ICommand 属性,用于处理 ComboBox 的 SelectionChanged 事件。 ```csharp private ICommand _comboBoxSelectionChangedCommand; public ICommand ComboBoxSelectionChangedCommand { get { if (_comboBoxSelectionChangedCommand == null) { _comboBoxSelectionChangedCommand = new RelayCommand<object>(ComboBoxSelectionChanged, CanComboBoxSelectionChanged); } return _comboBoxSelectionChangedCommand; } } private bool CanComboBoxSelectionChanged(object parameter) { // 可选:检查命令是否可用 return true; } private void ComboBoxSelectionChanged(object parameter) { // 处理 ComboBox 的 SelectionChanged 事件 } ``` 2. 在 XAML ,将 ComboBox 的 SelectionChanged 事件绑定到上面定义的命令,并使用 CommandParameter 属性将 ComboBox 本身作为参数传递给命令。 ```xml <ComboBox ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" SelectionChanged="{Binding ComboBoxSelectionChangedCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}"> <ComboBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding}" /> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> ``` 注意,在上面的代码,我使用了一个 RelayCommand 类来实现 ICommand 接口,你可以根据需要使用其他实现方式来绑定命令。另外,我还使用了 CommandParameter 属性将 ComboBox 本身作为参数传递给命令,这样在 ViewModel 就可以访问 ComboBox 的属性和方法了。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值