绑定基础知识


1 Binding 中的Model 取值5 种
mode有五种方式:
(1)OneWay 单向绑定
(2)TwoWay 双向绑定
(3)OneTime,最初根据源属性值设置目标属性,以后就忽略所有改变,就是说,只进行初始化,就一次
(4)OneWayToSource,这和OneWay相反
(5)Default,这是默认形式,就是双向绑定
1)当只想让用户看到数据,而不希望用户去修改数据时,可以采用 OneWay 模式,类似winform中的只读属性。
2)当希望用户可以对控件中的数据进行修改,同时让用户修改的数据更新到数据源(DataSet、对象、XML 或其他绑定控件)中时,可以使用 TwoWay 绑定。
3)如果想让用户修改数据源中的数据,而又不想使用TowWay模式,就可以使用 OneWayToSource 绑定。OneWayToSource模式允许通过在原来被看作是绑定源的对象中放置绑定表达式,从而翻转源和目标。
4)当你的界面中的一系列只读控件均被绑定了数据,并且当用户刷新了数据源时,希望绑定控件中的值仍保持不变,可以使用 OneTime 绑定。此外,当源没有实现 INotifyPropertyChanged 时,OneTime 绑定模式也是一个不错的选择。
说明:绑定目标中的修改何时去修改数据源
在上面的例子中,TextBox 使用了 TwoWay 绑定模式,所以当TextBox 失去焦点时WPF会使用TextBox中的值改变ListBox中的值。如果你不想在TextBox失去焦点时,就去修改ListBox中的值,可以为 UpdateSourceTrigger 指定值,它是用于定义何时更新源的绑定属性。可以为 UpdateSourceTrigger 设置三个值:Explicit、LostFocus 和 PropertyChanged。
如果将 UpdateSourceTrigger 设置为 Explicit,则不会更新源,除非从代码中调用 BindingExpression.UpdateSource 方法。设置为LostFocus ,(TextBox 控件的默认值)指示数据源绑定的控件失去焦点时才会更新。PropertyChanged 值绑定控件的绑定属性每次发生更改时就去更新数据源中的值
2 更新机制 Binding.UpdateSourceTrigger 4个枚举值 只会影响源数据,而不会影响目标数据
绑定中的默认更新机制,更新机制Binding.UpdateSourceTrigger,这个属性有4个枚举值
(1)PropertyChange,当值改变的时候,就更新。

(2)LostFocus,当时去焦点的时候更新. (默认是这种)
(3)Explicit,当调用BingingExpression.UpdateSource()方法的使用更新,其他情况不会更新。
(4)Default,默认形式
注意:以上这四种更新机制的设定,只会影响源数据,而不会影响目标数据。

1 RelativeSource 相对源
RelativeSource 是一种用于数据绑定的 MarkupExtension,它允许你引用视觉树中的其他元素作为数据绑定的源。-
Self: 使用Self可以引用当前元素自身作为数据绑定的源。<TextBlock Text="{Binding Text, RelativeSource={RelativeSource Self}}" /><Button Content="{Binding RelativeSource={RelativeSource Self}, Path=Height}"/>
FindAncestor: 使用 FindAncestor 可以向上搜索视觉树,查找最近的指定类型的祖先元素作为数据绑定的源。可以过 AncestorType 属性指定祖先元素的类型。
<TextBlock Text="{Binding Text, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TextBox}}}" />
<TextBlock Text="1" Width="40" Height="40" Background="Gray"> <i:Interaction.Triggers> <i:EventTrigger EventName="MouseLeftButtonDown"> <i:InvokeCommandAction Command="{Binding MouseLeftButtonDownCommand }" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type TextBlock}}}" /> </i:EventTrigger> </i:Interaction.Triggers> </TextBlock>
-
PreviousData: 使用PreviousData可以引用在上一个元素的ItemsControl中的数据作为数据绑定的源。这在使用ItemsControl的项模板中绑定数据时非常有用。<ItemsControl> <ItemsControl.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Name}" /> <TextBlock Text="{Binding PreviousData.Name, RelativeSource={RelativeSource PreviousData}}" /> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> -
TemplatedParent: 使用TemplatedParent可以引用控件模板中的父元素作为数据绑定的源。这在自定义控件的模板中绑定控件的属性时非常有用。<ControlTemplate TargetType="Button"> <Border Background="{Binding Background, RelativeSource={RelativeSource TemplatedParent}}"> <ContentPresenter /> </Border> </ControlTemplate>
RelativeSource 还提供其他一些属性和用法,可以根据需要进行调整。通过使用 RelativeSource,你可以方便地在视觉树中定位其他元素作为数据绑定的源,以实现更灵活和动态的数据绑定
比如 定义一个类 wBase,别的地方引用
xmlns:local="clr-namespace:Oge.IOTPlat.BLEProductFactory.UI"
<!--最大化按钮--
<Button x:Name="btnMax" Style="{StaticResource MenuButtonTemplate }" Content=""
Visibility="{Binding IsMaximizeButtonVisible,RelativeSource={RelativeSource AncestorType={x:Type local:wBase } } }" />
在DataGrid 中添加一个button
<DataGridTemplateColumn Header="操作" Width="160">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Content="重启" Width="30" Margin="2" Command="{Binding DataContext.ReBootClick,
RelativeSource={RelativeSource AncestorType=DataGrid,Mode=FindAncestor}}" CommandParameter="{Binding MAC}" />
2 ElementName
ElementName 使用 ElementName 绑定方式
<Button x:Name="myButton" Content="{Binding ElementName=myButton, Path=ActualWidth}"/>
3 代码绑定
<Button x:Name="myButton" Content="Click me"/>
public MainWindow()
{
InitializeComponent();
Binding binding = new Binding("ActualWidth");
binding.Source = myButton;
myButton.SetBinding(Button.ContentProperty, binding);
}
4 绑定资源 用Scurce


浙公网安备 33010602011771号