• 13、依赖属性


    例子一:简单的依赖属性

    <StackPanel>
            <TextBox Name="txtBox1"></TextBox>
            <TextBox Name="txtBox2"></TextBox>
            <Button Name="btnTest" Width="120" Height="36" Click="BtnTest_OnClick">OK</Button>
    </StackPanel>
        class Student:DependencyObject
        {
            //最简单的依赖属性
            public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof (string),
                                                                                                 typeof (Student));
        }
            private void BtnTest_OnClick(object sender, RoutedEventArgs e)
            {
                Student stu=new Student();
                stu.SetValue(Student.NameProperty,this.txtBox1.Text);
                this.txtBox2.Text = (string)stu.GetValue(Student.NameProperty);
            }


    程序的效果是:在txtBox1中输入内容,点击btnTest按钮,textBox2中Text获得属性值。实质上首先定义了个依赖属性,然后通过txtBox1修改属性值,最后txtBox2获取属性值。这里暂时看不出来依赖属性有啥奇特之处。

    例子二:将UI作为数据源,属性作为依赖目标

        <StackPanel>
            <TextBox Name="txtBox1"></TextBox>
            <Button Name="btnTest" Width="120" Height="36" Margin="5" Click="BtnTest_OnClick">Test</Button>
        </StackPanel>
            private Student stu;
            public MainWindow ( )
            {
                InitializeComponent ( );
                stu=new Student();
                Binding binding=new Binding("Text"){Source = txtBox1};
                BindingOperations.SetBinding(stu, Student.NameProperty, binding);
            }
    
            private void BtnTest_OnClick(object sender, RoutedEventArgs e)
            {
                MessageBox.Show(stu.GetValue(Student.NameProperty).ToString());
            }
            private Student stu;
            private Binding binding;
            public MainWindow ( )
            {
                InitializeComponent ( );
                stu=new Student();
                binding=new Binding("Text"){Source = txtBox1};
                BindingOperations.SetBinding(stu, Student.NameProperty, binding);
            }
    
            private void BtnTest_OnClick(object sender, RoutedEventArgs e)
            {
                MessageBox.Show(stu.GetValue(Student.NameProperty).ToString());
            }
    
            private void BtnTest1_OnClick(object sender, RoutedEventArgs e)
            {
                txtBox2.SetBinding ( TextBox.TextProperty,binding );
            }

    现在我们使用的依赖属性依靠GetValue和SetValue两个方法进行对外界的暴露,而且在使用GetValue的时候还需要进行一次数据类型的转换。

    例子三:为依赖属性添加一个CLR属性外包装

        class Student:DependencyObject
        {
            //依赖属性
            public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof (string),
                                                                                                 typeof (Student));
            //CLR属性包装器
            public string Name
            {
                get { return (string) GetValue(NameProperty); }
                set{SetValue(NameProperty,value);}
            }
            
            //SetBinding包装
            public BindingExpressionBase SetBinding(DependencyProperty dp, BindingBase binding)
            {
                return BindingOperations.SetBinding(this, dp, binding);
            }
    
        }
            <TextBox Name="txtBox1" Text="你是谁" ></TextBox>
            <TextBox Name="txtBox2"></TextBox>
            private Student stu;
            public MainWindow ( )
            {
                InitializeComponent ( );
                stu=new Student();
                stu.SetBinding(Student.NameProperty, new Binding("Text") {Source = txtBox1});
                txtBox2.SetBinding(TextBox.TextProperty, new Binding("Name") {Source = stu});
            }

    运行程序,当在txtBox1中输入字符的时候,txtBox2就会同步显示,此时Student对象的Name属性值也同步变化了。

    了解依赖属性存取值得秘密

     依赖属性多用在自定义控件和扩展已有空间的某些功能-小灰猫说的

  • 相关阅读:
    PID控制算法原理(抛弃公式,从本质上真正理解PID控制)(转)
    用三张图片详解Asp.Net 全生命周期
    Maven 3 入门 核心概念
    Maven 3 入门 HelloWorld
    Spring 3.x MVC 入门3 使用内容协商来实现多视图
    Nosql之Mongodb 1 安装配置与基本操作
    Spring 3.x MVC 入门31 使用内容协商来实现多视图 示例
    Nosql之Mongodb 2 高级查询
    Maven 3 入门 如何创建一个web应用程序
    Spring 3.x MVC 入门4 @ResponseBody & @RequestBody
  • 原文地址:https://www.cnblogs.com/chenyongblog/p/3492052.html
Copyright © 2020-2023  润新知