WPF数据绑定-简单对象的绑定

绑定自定义的数据类对象


在xaml代码中,Binding标记扩展中仅定义了Path属性,将它绑定到StudentData类的属性上。不需要定义源对象,因为通过指定DataContext类定义源对象。

DataContext是一个依赖属性,它用基于FramewrokElement定义。指定相应控件的DataContext属性表示当前控件中的每个元素都默认绑定此数据。


xaml代码

[csharp]  view plain  copy
  1. <Window x:Class="BindingDemo.Window2"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         Title="Window2" Height="300" Width="300">  
  5.     <Grid>  
  6.         <!-- 简单对象的绑定 -->  
  7.         <StackPanel Name="stackPanel" HorizontalAlignment="Left" VerticalAlignment="Top" Orientation="Vertical">  
  8.             <Label Name="lbId" Content="{Binding ID}"/>  
  9.             <Label Name="lbName" Content="{Binding Name}"/>  
  10.             <Label Name="lbAge" Content="{Binding Age}"/>  
  11.         </StackPanel>  
  12.     </Grid>  
  13. </Window>  

数据类

[csharp]  view plain  copy
  1. namespace BindingDemo  
  2. {  
  3.     public class StudentData  
  4.     {  
  5.         public int ID { getset; }  
  6.         public string Name { getset; }  
  7.         public int Age { getset; }  
  8.     }  
  9. }  

隐藏代码

[csharp]  view plain  copy
  1. public partial class Window2 : Window  
  2. {  
  3.     public Window2()  
  4.     {  
  5.         InitializeComponent();  
  6.   
  7.         Init();  
  8.     }  
  9.   
  10.     public void Init()  
  11.     {  
  12.         StudentData stuData = new StudentData();  
  13.         stuData.ID = 1001;  
  14.         stuData.Name = "小明";  
  15.         stuData.Age = 18;  
  16.   
  17.         //this.DataContext = stuData;//整个窗口内的所有元素都可以绑定此数据  
  18.         stackPanel.DataContext = stuData;//仅stackPanel内的所有元素可以绑定此数据  
  19.     }  
  20. }  


以上绑定当修改数据内容时界面显示是不会更改的,要实现更改信息传递给用户界面,数据类必须实现INotifyPropertyChanged接口。

该接口定义了ProperytChanged事件,该事件在OnPropertyChagned方法中触发。

xaml代码

[csharp]  view plain  copy
  1. <Window x:Class="BindingDemo.Window2"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         Title="Window2" Height="300" Width="300">  
  5.     <Grid>  
  6.         <!-- 简单对象的绑定 -->  
  7.         <StackPanel Name="stackPanel" HorizontalAlignment="Left" VerticalAlignment="Top" Orientation="Vertical">  
  8.             <Label Name="lbId" Content="{Binding ID}"/>  
  9.             <Label Name="lbName" Content="{Binding Name}"/>  
  10.             <Label Name="lbAge" Content="{Binding Age}"/>  
  11.               
  12.             <Button Name="btnChang" Content="按钮" Click="btnChang_Click"/>  
  13.         </StackPanel>  
  14.     </Grid>  
  15. </Window>  

数据类

[csharp]  view plain  copy
  1. namespace BindingDemo  
  2. {  
  3.     public class StudentData : INotifyPropertyChanged  
  4.     {  
  5.         private int _id = 0;  
  6.         private string _name = "";  
  7.         private int _age = 0;  
  8.   
  9.         public int ID   
  10.         {   
  11.             get { return _id; }   
  12.             set   
  13.             {   
  14.                 _id = value;  
  15.   
  16.                 OnPropertyChanged("ID");  
  17.             }   
  18.         }  
  19.         public string Name  
  20.         {  
  21.             get { return _name; }  
  22.             set   
  23.             {   
  24.                 _name = value;  
  25.                 OnPropertyChanged("Name");  
  26.             }  
  27.         }  
  28.         public int Age   
  29.         {  
  30.             get { return _age; }  
  31.             set   
  32.             {   
  33.                 _age = value;  
  34.                 OnPropertyChanged("Age");  
  35.             }  
  36.         }  
  37.   
  38.         public event PropertyChangedEventHandler PropertyChanged;  
  39.         protected void OnPropertyChanged(string propertyName)  
  40.         {  
  41.             if(PropertyChanged != null)  
  42.             {  
  43.                 PropertyChanged(thisnew PropertyChangedEventArgs(propertyName));  
  44.             }  
  45.         }  
  46.     }  
  47. }  

隐藏代码

[csharp]  view plain  copy
  1. public partial class Window2 : Window  
  2. {  
  3.     public Window2()  
  4.     {  
  5.         InitializeComponent();  
  6.   
  7.         Init();  
  8.     }  
  9.   
  10.     private StudentData stuData;  
  11.     public void Init()  
  12.     {  
  13.         stuData = new StudentData();  
  14.         stuData.ID = 1001;  
  15.         stuData.Name = "小明";  
  16.         stuData.Age = 18;  
  17.   
  18.         //this.DataContext = stuData;//整个窗口内的所有元素都可以绑定此数据  
  19.         stackPanel.DataContext = stuData;//仅stackPanel内的所有元素可以绑定此数据  
  20.     }  
  21.   
  22.     private void btnChang_Click(object sender, RoutedEventArgs e)  
  23.     {  
  24.         stuData.ID = 1002;  
  25.         stuData.Name = "小红";  
  26.         stuData.Age = 17;  
  27.     }  
  28. }  

此时运行程序点击按钮更改信息时发现用户界面显示的数据也跟着刷新了。下面的内容是对上面的数据类做的进一步封装。最终效果是一样的。


先创建一个实现INotifyPropertyChanged接口的一个抽象类基类BindableObject,数据类只需要继承此抽象基类自然就实现了接口INotifyPropertyChanged

[csharp]  view plain  copy
  1. namespace BindingDemo  
  2. {  
  3.     public abstract class BindableObject : INotifyPropertyChanged  
  4.     {  
  5.         public event PropertyChangedEventHandler PropertyChanged;  
  6.         protected void OnPropertyChanged(string propertyName)  
  7.         {  
  8.             if (PropertyChanged != null)  
  9.             {  
  10.                 PropertyChanged(thisnew PropertyChangedEventArgs(propertyName));  
  11.             }  
  12.         }  
  13.   
  14.         protected void SetProperty<T>(ref T item,T value,[CallerMemberName] string propertyName=null)  
  15.         {  
  16.             if(!EqualityComparer<T>.Default.Equals(item,value))  
  17.             {  
  18.                 item = value;  
  19.                 OnPropertyChanged(propertyName);  
  20.             }  
  21.         }  
  22.     }  
  23. }  


[csharp]  view plain  copy
  1. namespace BindingDemo  
  2. {  
  3.     public class StudentData : BindableObject  
  4.     {  
  5.         private int _id = 0;  
  6.         private string _name = "";  
  7.         private int _age = 0;  
  8.   
  9.         public int ID   
  10.         {   
  11.             get { return _id; }   
  12.             set   
  13.             {   
  14.                 SetProperty(ref _id,value);  
  15.             }   
  16.         }  
  17.         public string Name  
  18.         {  
  19.             get { return _name; }  
  20.             set   
  21.             {  
  22.                 SetProperty(ref _name, value);  
  23.             }  
  24.         }  
  25.         public int Age   
  26.         {  
  27.             get { return _age; }  
  28.             set   
  29.             {  
  30.                 SetProperty(ref _age, value);  
  31.             }  
  32.         }  
  33.     }  
  34. }  


[CallerMemberName]获取调用方的属性或方法名称

EqualityComparer<T>    T为要比较的对象的类型

EqualityComparer<T>.Default  返回一个默认的相等比较器,用于比较此泛型自变量指定的类型。


转自 :http://blog.csdn.net/i1tws/article/details/67655880

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值