13、依存属性

11276 ワード

例1:単純な依存属性
<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);

        }

プログラムの効果は、txtBox 1に内容を入力し、btnTestボタンをクリックし、textBox 2のTextで属性値を取得することです.実質的にはまず依存属性を定義し,その後txtBox 1で属性値を変更し,最後にtxtBox 2で属性値を取得する.ここでは依存属性に何か奇妙な点がしばらく見えない.
例2: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の2つの方法によって外部への暴露を行い,GetValueを使用する場合にはデータ型の変換を1回行う必要がある.
例3:依存属性に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});

        }

プログラムを実行し、txtBox 1に文字を入力すると、txtBox 2が同期して表示され、StudentオブジェクトのName属性値も同期して変化します.
 
依存属性アクセスの秘密を理解する
依存プロパティは、カスタムコントロールや既存のスペースを拡張するいくつかの機能に多く使用されます.