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属性值也同步变化了。

 

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

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

 

 

 

 

 

你可能感兴趣的:(属性)