数据绑定

本文深入探讨了WPF数据绑定的原理与实践,包括数据绑定模型的五个关键组成部分,数据绑定的方向及其设置方法,以及如何实现随数据源属性变化的目标对象更新。同时,介绍了UpdateSourceTrigger的不同设置对数据绑定的影响,以及四种常见的数据源绑定方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

WPF的数据绑定

1)数据绑定模型

数据绑定模型由五部分组成:DependencyObject(目标对象)、目标属性、Object(数据源对象)、数据源属性以及起连接作用的Binding的绑定对象。

2)数据的绑定方向

  1. 通过Binding 的Mode属性设置数据绑定的方向
    OneWay:从数据源到目标对象即目标对象会随数据源改变而改变;
    TwoWay:双向改变;
    OneWayToSource:从目标对象到数据源即通过目标对象更新数据源对象的某些属性。
  2. 实现随数据源属性改变的DependencyObject更新
    数据源实现INotifyPropertyChanged接口分两步:
    第一步:让该类继承接口INotifyPropertyChanged接口并实现PropertyChanged事件
    第二步:设置属性在每一次调用时使用Notify()函数触发PropertyChanged事件
 <Grid>
        <Button Content="修改" HorizontalAlignment="Left" Margin="104,142,0,0" VerticalAlignment="Top" Width="94" Height="27" Click="Button_Click" />
        <Button Content="下一个" HorizontalAlignment="Left" Margin="313,142,0,0" VerticalAlignment="Top" Width="89" Height="27"/>
        <TextBlock HorizontalAlignment="Left" Margin="130,46,0,0" TextWrapping="Wrap" Text="姓名:" VerticalAlignment="Top" Height="23" Width="68" FontSize="16"/>
        <TextBox Name="txbName" HorizontalAlignment="Left" Height="23" Margin="268,46,0,0" TextWrapping="Wrap" Text="{Binding Source={StaticResource ronger},Path=Name}" VerticalAlignment="Top" Width="120"/>
        <TextBlock HorizontalAlignment="Left" Margin="130,86,0,0" TextWrapping="Wrap" Text="年龄:" VerticalAlignment="Top" Height="23" Width="68" FontSize="16"/>
        <TextBox Name="txbAge" HorizontalAlignment="Left" Height="23" Margin="268,86,0,0" TextWrapping="Wrap" Text="{Binding Source={StaticResource ronger},Path=Age,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
    </Grid>
public class Person:INotifyPropertyChanged
    {
        string name;
        public event PropertyChangedEventHandler PropertyChanged;
        void Notify(string proName)
        {
            if (this.PropertyChanged!=null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(proName));
            }
        }
        public string Name
        {
            get { return this.name; }
            set
            {
                if (this.name == value) { return; }
                this.name = value;
                Notify("Name");
            }
        }
        int age;
        public int Age
        {
            get { return this.age; }
            set
            {
                if (this.age == value) { return; }
                this.age = value;
                Notify("Age");//触发修改事件
            }
        }
        public Person() { }
        public Person( string name,int age)
        {
            this.age = age;
            this.name = name;
        }
    }
  1. 数据绑定的触发条件
    在这里插入图片描述
    UpdateSourceTrigger的设置
    (1)LostFocus:目标对象失去焦点时更新数据源
    (2)PropertyChanged:更新数据源的属性时DependencyObject的属性才会改变
    (3)Explicit:只有显示通知才会更新数据源
    (4)Default:默认和LostFocus相同
  2. 绑定数据源的几种方式:
    (1)Source:适用于数据源为普通的.Net对象
    (2)Element:多用于元素之间的绑定,通过滑块控件改变文本框字字体大小
    (3)RelativeSource:如果数据源需要指定相对位置时(FindAncestor、Self、PreviousData、TemplatedParent)
     FindAncestor:沿着元素树向上查找
     Self:数据源自生为目标对象,用来绑定目标对象的A属性和B属性
     PreviousData:多用在列表中,表示前一个列表项
    (4)TemplatedParent:表示拥有该模板的父类
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值