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

系统 1735 0

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

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


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论