心澄欲遣

不践迹,亦不入于室

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

下面代码是常用的包装方式,是死的,好好练熟,使劲敲。

 public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name",typeof(string),typeof(Student),new UIPropertyMetadata("王俊鹏"));

        public string Name
        {
            get { return (string)GetValue(NameProperty); }
            set { SetValue(NameProperty, value); }
        }
View Code

有快捷方式的,propa Get/Set 附加属性用的;propdp,常用包装。

下面的全部的依赖属性代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;

namespace DependencyTest
{
    class Student : DependencyObject
    {

        public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(Student), new UIPropertyMetadata("王俊鹏"));

        public string Name
        {
            get { return (string)GetValue(NameProperty); }
            set { SetValue(NameProperty, value); }
        }

        private static void OnChangeAgeValue(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            MessageBox.Show("Change NewAge is" + e.NewValue.ToString() + " OLD AGE is" + e.OldValue.ToString());
        }

        private static object OnCoerceValue(DependencyObject d, object value)
        {
            MessageBox.Show("Coerce Age is" + value.ToString());
            return value;
        }

        private static bool IsValidateValue(object value)
        {
            Console.WriteLine("ValidateValue value is {0}", value);
            return true;
        }

        public static readonly DependencyProperty AgeProperty = DependencyProperty.Register("Age", typeof(int), typeof(Student),
            new FrameworkPropertyMetadata((int)100,
            FrameworkPropertyMetadataOptions.None,
            new PropertyChangedCallback(OnChangeAgeValue),
            new CoerceValueCallback(OnCoerceValue)),
            new ValidateValueCallback(IsValidateValue));

        public int Age
        {
            get { return (int)GetValue(AgeProperty); }
            set { SetValue(AgeProperty, value); }
        }

        /// <summary>
        /// 把其它控件的属性绑定到自身的依赖属性上
        /// </summary>
        /// <param name="dp">自身依赖属性</param>
        /// <param name="bind">其它控件的属性</param>
        /// <returns></returns>
        public BindingExpressionBase SetBinding(DependencyProperty dp, Binding bind)
        {
            return BindingOperations.SetBinding(this, dp, bind);
        }
    }
    /// <summary>
    /// 要附加的属性
    /// </summary>
    class School : DependencyObject
    {
        public static int GetGrade(DependencyObject obj)
        {
            return (int)obj.GetValue(GradeProperty);
        }

        public static void SetGrade(DependencyObject obj, object value)
        {
            obj.SetValue(GradeProperty, value);
        }

        // Using a DependencyProperty as the backing store for GetGrade.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty GradeProperty =
            DependencyProperty.RegisterAttached("Grade", typeof(int), typeof(School));

    }
}
View Code

下面是前台调用的几种方式以及附加属性

 private void button1_Click(object sender, RoutedEventArgs e)
        {
            Student stu = new Student();
            //附加属性
            School.SetGrade(stu, 6);
            this.label1.Content = School.GetGrade(stu).ToString();
            //1
            //stu.Age = Convert.ToInt32(this.textBox1.Text.Trim());
            //this.textBox2.Text = stu.Age.ToString();

            //2
            //Binding bindAge = new Binding("Text") { Source = this.textBox1 };
            //BindingOperations.SetBinding(stu, Student.AgeProperty, bindAge);
            //this.textBox2.Text = ((int)stu.GetValue(Student.AgeProperty)).ToString();

            //3 最牛逼的方式了
            Binding bindAge = new Binding("Text") {Source=this.textBox1 };
            stu.SetBinding(Student.AgeProperty, bindAge);
            this.textBox2.Text = stu.Age.ToString();
           
            //4
            //stu.SetValue(Student.AgeProperty, Convert.ToInt32(this.textBox1.Text.Trim()));
            //this.textBox2.Text = ((int)stu.GetValue(Student.AgeProperty)).ToString();
        }
View Code

 

posted on 2013-06-24 09:48  心澄欲遣  阅读(340)  评论(0编辑  收藏  举报
欢迎第myspace graphics个访客