DependencyProperty使用中的3个场景,讨论PropertyChangedCallback

简介: 1:项目结构图 2:控件SilverlightControl1 前台: 后台: public partial class SilverlightControl1 : UserControl { public SilverlightControl1(...

1:项目结构图

image

2:控件SilverlightControl1

前台:

image

后台:

    public partial class SilverlightControl1 : UserControl
    {
        public SilverlightControl1()
        {
            InitializeComponent();
        }

        public static readonly DependencyProperty StudentProperty = DependencyProperty.Register("Student", typeof(Student), typeof(SilverlightControl1), new PropertyMetadata(new PropertyChangedCallback(xxx)));

        public Student Student
        {
            get
            {
                return (Student)GetValue(StudentProperty);
            }
            set
            {
                SetValue(StudentProperty, value);
                
            }
        }

        static void xxx(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            MessageBox.Show("sss");
        }
    }

3:调用方

前台:

image

后台:

    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        Student student1 = new Student() { StudentName = "hzh" + DateTime.Now.ToFileTime(), Age = 99 };
        Student student2 = new Student() { StudentName = "lmj" + DateTime.Now.ToFileTime(), Age = 99 };
        Student student3 = new Student() { StudentName = "lh" + DateTime.Now.ToFileTime(), Age = 99 };

        //初始化
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //采用非绑定机制
            uc1.Student = student1;
            //采用非绑定机制,对DataContext赋值
            uc2.DataContext = student2;
            //采用绑定机制
            uc3.DataContext = student3;
        }

        //改Model值
        private void Button_Click1(object sender, RoutedEventArgs e)
        {
            student1.StudentName = "lmj" + DateTime.Now.ToString();
            student1.Age = 90;
            student2.StudentName = "hzh" + DateTime.Now.ToString();
            student2.Age = 90;
            student3.StudentName = "hzh" + DateTime.Now.ToString();
            student3.Age = 90;
        }

        //更换Model
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            student1 = new Student() { StudentName = "hzh" + DateTime.Now.ToFileTime(), Age = 99 };
            student2 = new Student() { StudentName = "lmj" + DateTime.Now.ToFileTime(), Age = 99 };
            student3 = new Student() { StudentName = "lh" + DateTime.Now.ToFileTime(), Age = 99 };
        }
    }

4:结论

无论是第一种方式,还是第三种方式,都能触发PropertyChangedCallback,而如果调用采用第二种方式,则不会触发PropertyChangedCallback。

源码:SilverlightApplication320110609.rar

Creative Commons License本文基于 Creative Commons Attribution 2.5 China Mainland License发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的署名 http://www.cnblogs.com/luminji(包含链接)。如您有任何疑问或者授权方面的协商,请给我留言。
目录
相关文章
Object.defineProperty熬夜整理的用法,保证你看的明白!
Object.defineProperty熬夜整理的用法,保证你看的明白!
Object.defineProperty熬夜整理的用法,保证你看的明白!
|
测试技术 索引
【类型挑战】实现 Readonly,难度⭐️
【类型挑战】实现 Readonly,难度⭐️
180 0
【类型挑战】实现 Readonly,难度⭐️
|
测试技术
【类型挑战】Readonly 2,难度⭐️⭐️
【类型挑战】Readonly 2,难度⭐️⭐️
142 0
【类型挑战】Readonly 2,难度⭐️⭐️
|
JavaScript
手摸手一起学习Typescript第二天,interface接口和readonly属性
手摸手一起学习Typescript第二天,interface接口和readonly属性
|
C#
WPF中监视DependencyProperty的变化
原文:WPF中监视DependencyProperty的变化               WPF中监视DependencyProperty的变化                      周银辉   尽管一个类会提供很多事件,但有时候还是显得不够,比如说前两天我就以为WPF的ListBox控件会有ItemsSourceChanged事件,但好像没有。
676 0
|
Web App开发 JavaScript 程序员