zoukankan      html  css  js  c++  java
  • DependencyProperty使用中的3个场景,讨论PropertyChangedCallback

    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

  • 相关阅读:
    文件的序列化和反序列化
    三个小功能,游戏倒计时,文件的序列化和反序列化,txt文档的读取和写入
    Unity 中Debug打印的全局注释方式和重写
    导航制作的几个步骤
    Unity中删除文件目录下的所有文件和查看文件里面的内容
    VS2017一些小技巧
    在Unity中图标进行鼠标图标更换
    Electron-Vue 使用 oss 实现上传、下载
    Electron-Vue 调用本地数据库
    构建 Electron-Vue 脚手架项目
  • 原文地址:https://www.cnblogs.com/luminji/p/2076330.html
Copyright © 2011-2022 走看看