DependencyProperty使用中的3个场景,讨论PropertyChangedCallback
来源:互联网 发布:日本程序员 编辑:程序博客网 时间:2024/06/15 21:26
1:项目结构图
2:控件SilverlightControl1
前台:
后台:
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:调用方
前台:
后台:
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
- dependencyProperty
- DependencyProperty
- PhysX 3.2中的场景查询(3)-使用
- WPF 使用依赖属性(DependencyProperty) 定义用户控件中的Image Source属性
- WPF里的DependencyProperty(3)
- WPF里的DependencyProperty(3)
- WPF里的DependencyProperty(3)
- WPF里的DependencyProperty(3)
- jQuery的ajax使用场景讨论(c#)
- jQuery的ajax使用场景讨论(c#)
- WPF中的DependencyProperty存储方式详解
- JAVA 中的LinkedHashMap 使用场景
- 讨论:GetMessage()的第二个参数使用
- MongoDB 3: 使用中的问题,及其应用场景
- 使用 WebSockets 的 9 个应用场景
- 使用 WebSockets 的 9 个应用场景
- 使用 WebSockets 的 9 个应用场景
- SaaS模式的中小企业客户关系管理研究
- 使用MVVM模式开发自定义UserControl
- 云聚合:物联网商业模式的可行选择
- WPF中的EventTrigger和命令绑定
- 【转】浅谈物联网的商业模式和经济模式
- DependencyProperty使用中的3个场景,讨论PropertyChangedCallback
- c++ STL关联式容器的共性(权哥)
- 解决 linux下Firefox 不能加载 Applet 的问题
- 使用Entity Framework和WCF Ria Services开发SilverLight之1:简单模型
- 使用Entity Framework和WCF Ria Services开发SilverLight之2:POCO
- 世界500强最爱的25个刁钻面试题
- 使用Entity Framework和WCF Ria Services开发SilverLight之3:Map
- SQLServer2008安装报Restart computer failed的解决办法
- SilverLight单元测试1:框架