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:結論
無論是第一種方式,還是第三種方式,都能觸發(fā)PropertyChangedCallback,而如果調用采用第二種方式,則不會觸發(fā)PropertyChangedCallback。
更多文章、技術交流、商務合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

