WPF之数据绑定

2024-08-22  本文已影响0人  小羊爱学习

绑定类型

ElementName:依据Name相互绑定的
将textbox的文本内容绑定到button上(当textbox文本发生变化时,button内容也会跟着变化)
ElementName : 确定绑定的元素
path : 绑定的属性

<StackPanel Orientation="Vertical">
    <TextBox Height="30"
             Width="100"
             BorderBrush="Black" Name="t"></TextBox>
    <Button Width="100"
            Height="40"
            Content="{Binding ElementName=t,Path=Text}"></Button>
</StackPanel>

RelativeSource:相对于本身或者父元素
Mode : 设置绑定的元素是本身还是父元素 (值 : Self本身 FindAncestor祖先)
AncestorType: 设置绑定父元素的类型
Path: 绑定的属性

<StackPanel Margin="0,100,0,0">
    <Button Height="40"
            FontSize="15"
            Content="{Binding RelativeSource={RelativeSource Mode=Self},Path=Height}" />
    <Button Height="40"
            FontSize="15"
            Content="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=StackPanel},Path=Margin}" />
</StackPanel>

ltemSouce:绑定到集合元素

        <ListBox Name="lsname" Width="200" Height="100" />
            ObservableCollection<string> list = new ObservableCollection<string>();
            list.Add("李四");
            list.Add("张三");
            list.Add("王五");
            lsname.ItemsSource = list;

DataContext:多种不同值绑定

<Grid Name="g"
      DataContext="{Binding}">
    <Button Width="100" Height="40" Content="{Binding Path=One}" ></Button>
    <Button Width="100" Height="40" Content="{Binding Path=Two}"></Button>
</Grid>
this.g.DataContext = new ClassName(){One = "我是One", Two = "我是two" };

绑定模式

属性名 含义
OneWay 源属性变化时更新目标属性
TwoWay 当源数据变化时更新目标属性, 并且当目标属性变化时更新源属性
OneTime 最初根据源属性值设置目标属性。然而, 在此之后的所有改变都会被忽略(除非绑定被设定为一个完全不同的对象或者调用BindingExpression.UpdateTarget()方法, 如稍后所介绍的那样)。通常, 如果知道源属性不会变化, 可以使用这种模式降低开销。
OneWayToSource 和OneWay 类似, 但是方向相反。当目标属性变化时更新源属性(这看起来有点像向后传递),但目标属性永远不会更新。
Default 此类绑定依赖于目标属性。它既可以是双向的(对于用户设置的属性, 如Textbox.Text属性),也可以是单向的(对于所有其他属性)。除非明确指定了另外一种模式, 否则所有绑定都使用该方法。

大多数属性都默认为 OneWay 绑定,但是一些依赖项属性,通常为用户可编辑的控件的属性,如 TextBox 的 Text 属性和 CheckBox 的 IsChecked 属性,默认为 TwoWay 绑定。

    <TextBox Width="150" Height="25"  Grid.Row="0" Grid.Column="1"  Name="textBox4" Text="{Binding ElementName=scrollBar1,Path=Value,Mode=TwoWay}" />

绑定更新:UpdateSourceTrigger

属性名 含义
propertyChanged 当目标属性发生变化时立即更新源目标
LostFocus 当目标属性发生变化时并且目标丢失焦点是更新源目标
Explicit 除非调用BindingExpression.UpdateSource()方法,否则无法更新资源
Default 根据目标属性的元数据确定更新行为, 大多数属性的默认行为是PropertyChanged, 但是TextBox.Text的属性默认行为是LoastFocus

注释:多数依赖项属性的UpdateSourceTrigger 值的默认值为 PropertyChanged,而 Text 属性的默认值为 LostFocus。
示例, 对于TextBox控件, 添加 UpdateSourceTrigger=PropertyChanged 以达到更新源目标, 但是往往在实际应用中, TextBox会频繁的变化, 从而导致多次更新源目标,PropertyChenged更新反而会是应用程序运行更加缓慢, 要完全控制源对象的更新时机, 可以选择 Explicit模式, 例如, 添加一个按钮, 在按钮中调用UpdateSource方法已达到更新的目的.

        <Button Name="btn" Width="200" Height="100" Click="btn_Click"></Button>
        <TextBlock Text="textblock" Name="txtfont"/>
        <TextBox Text="{Binding ElementName=txtfont,Path=FontSize,UpdateSourceTrigger=PropertyChanged}"/>

注:在调用UpdateSource之前, 需要通过GetBindingExpression()方法获取到BindingExpression对象, 从而调用UpdateSource()方法。

        private void btn_Click(object sender, RoutedEventArgs e)
        {
            BindingExpression exp = txtfont.GetBindingExpression(TextBlock.FontSizeProperty);
            exp.UpdateSource();
        }
上一篇下一篇

猜你喜欢

热点阅读