WPF 用依赖属性实现一个连锁的依赖关系

如果我们要实现:一个自定义对象O的某个属性依赖到控件C1上,同时,当这个对象的这个属性发生变化时,关注此对象的控件C2的显示内容也要改变,这看起来就像是一个连锁反应-C2依赖于O,O依赖于C1。


要实现这个功能我们先实现C2依赖于O这一部分。既然O这个对象要让其他控件依赖,那么O必须在自己的属性发生变化的时候通知其他控件,因此,这个O对象必须实现INotifyPropertyChanged接口,示例代码如下:


   public class Employee : INotifyPropertyChanged
    {
        //make the object has capability to inform other objects
        public event PropertyChangedEventHandler PropertyChanged;


        private string name;
        public string Name
        {
            get { return name; }
            set
            { 
                name = value;
                //inform other objects which depend on this property
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name"));
            }
        }
        public int Age {get;set;}
    }


在示例代码中,当Name属性的值发生变化之后,触发事件通知依赖于此属性的对象

假设界面上有一个txt1文本框,让文本框的Text属性依赖于Employee实例的Name属性的代码如下:

        Employee em;
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
           em = new Employee();
            Binding binding=new Binding("Name"){Source=em};
            BindingOperations.SetBinding(txt1, TextBox.TextProperty, binding);
        }

如果此时你用另外一个方法去改变em对象的Name属性,txt1文本框会马上显示相应改变的内容。


好,现在实现O依赖于C1部分。

在这个例子里, 为了让Employee 实例依赖于其他对象的属性,我们需要为Employee创建依赖属性,在这个例子里头及要让Name有能力依赖于另外对象的属性,要实现这个功能,这个类必须继承自DependencyObject类。因此我们修改我们的Employee类如下:

  public class Employee : DependencyObject,INotifyPropertyChanged
    {
        //make the object has capability to inform other objects
        public event PropertyChangedEventHandler PropertyChanged;


        private string name;
        public string Name
        {
            get { return name; }
            set
            { 
                name = value;
                //inform other objects which depend on this property
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name"));
            }
        }
        public int Age {get;set;}


        //register the dependency property
        public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(Employee));
    }


同时修改Window Load 事件内容

      private void Window_Loaded(object sender, RoutedEventArgs e)
        {
           em = new Employee();
            Binding binding1=new Binding("Name"){Source=em};
            BindingOperations.SetBinding(txt1, TextBox.TextProperty, binding1);


            Binding binding2 = new Binding("Text") { Source = txt2};
            BindingOperations.SetBinding(em, Employee.NameProperty, binding2);
        }

这样,在txt2中改变字符内容的时候em对象的Name属性也会相应改变,em对象会同时通知txt1改变值。


前台代码如下:

    <StackPanel>
        <TextBox x:Name="txt1" Margin="5"></TextBox>
        <TextBox x:Name="txt2" Margin="5"></TextBox>
    </StackPanel>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值