您的位置:首页 > 其它

WPF 中双向绑定通知机制之ObservableCollection使用

2015-07-15 11:06 585 查看
msdn中 ObservableCollection<T> 类 表示一个动态数据集合,在添加项、移除项或刷新整个列表时,此集合将提供通知。

在许多情况下,所使用的数据是对象的集合。 例如,数据绑定中的一个常见方案是使用 ItemsControl(如 ListBox、ListView 或 TreeView)来显示记录的集合。

可以枚举实现 IEnumerable 接口的任何集合。 但是,若要设置动态绑定,以便集合中的插入或删除操作可以自动更新 UI,则该集合必须实现 INotifyCollectionChanged 接口。 此接口公开 CollectionChanged 事件,只要基础集合发生更改,都应该引发该事件。

WPF 提供 ObservableCollection<T> 类,它是实现 INotifyCollectionChanged 接口的数据集合的内置实现。

还有许多情况,我们所使用的数据只是单纯的字段或者属性,此时我们需要为这些字段或属性实现INotifyPropertyChanged接口,实现了该接口,只要字段或属性的发生了改变,就会提供通知机制。

ObservableCollection<T>实现

前台xmal

view
sourceprint?

01.
<Window
x:Class=
'WpfApplication1.WindowObservable'


02.
xmlns=
'http://schemas.microsoft.com/winfx/2006/xaml/presentation'


03.
xmlns:x=
'http://schemas.microsoft.com/winfx/2006/xaml'


04.
Title=
'Window8'
Height=
'356'
Width=
'471'
>


05.
<Grid>


06.
<StackPanel
Height=
'295'
HorizontalAlignment=
'Left'
Margin=
'10,10,0,0'
Name=
'stackPanel1'
VerticalAlignment=
'Top'
Width=
'427'
>


07.
<TextBlock
Height=
'23'
Name=
'textBlock1'
Text=
'学员编号:'
/>


08.
<TextBox
Height=
'23'
Name=
'txtStudentId'
Width=
'301'
HorizontalAlignment=
'Left'
/>


09.
<TextBlock
Height=
'23'
Name=
'textBlock2'
Text=
'学员列表:'
/>


10.
<ListBox
Height=
'156'
Name=
'lbStudent'
Width=
'305'
HorizontalAlignment=
'Left'
>


11.
<ListBox.ItemTemplate>


12.
<DataTemplate>


13.
<StackPanel
Name=
'stackPanel2'
Orientation=
'Horizontal'
>


14.
<TextBlock
Text=
'{Binding
Id,Mode=TwoWay}'
Margin=
'5'
Background=
'Beige'
/>


15.
<TextBlock
Text=
'{Binding
Name,Mode=TwoWay}'
Margin=
'5'
/>


16.
<TextBlock
Text=
'{Binding
Age,Mode=TwoWay}'
Margin=
'5'
/>


17.
</StackPanel>


18.
</DataTemplate>


19.
</ListBox.ItemTemplate>


20.
</ListBox>


21.
<Button
Content=
'Button'
Height=
'23'
Name=
'button1'
Width=
'75'
HorizontalAlignment=
'Left'
Click=
'button1_Click'
/>


22.
</StackPanel>


23.
</Grid>


24.
</Window>


后台cs

view
sourceprint?

01.
using
System;


02.
using
System.Collections.Generic;


03.
using
System.Linq;


04.
using
System.Text;


05.
using
System.Windows;


06.
using
System.Windows.Controls;


07.
using
System.Windows.Data;


08.
using
System.Windows.Documents;


09.
using
System.Windows.Input;


10.
using
System.Windows.Media;


11.
using
System.Windows.Media.Imaging;


12.
using
System.Windows.Shapes;


13.
using
System.Collections.ObjectModel;


14.
using
System.ComponentModel;


15.


16.
namespace
WpfApplication1


17.
{


18.
public
partial
class
WindowObservable
: Window


19.
{


20.
ObservableCollection<Students>
infos =
new
ObservableCollection<Students>()
{


21.
new
Students(){
Id=
1
,
Age=
11
,
Name=
'Tom'
},


22.
new
Students(){
Id=
2
,
Age=
12
,
Name=
'Darren'
},


23.
new
Students(){
Id=
3
,
Age=
13
,
Name=
'Jacky'
},


24.
new
Students(){
Id=
4
,
Age=
14
,
Name=
'Andy'
}


25.
};


26.


27.
public
WindowObservable()


28.
{


29.
InitializeComponent();


30.


31.
this
.lbStudent.ItemsSource
= infos;


32.


33.
this
.txtStudentId.SetBinding(TextBox.TextProperty,
new
Binding(
'SelectedItem.Id'
)
{ Source = lbStudent });


34.
}


35.
private
void
button1_Click(object
sender,RoutedEventArgs e)


36.
{


37.
infos[
1
]
=
new
Students()
{ Id =
4
,
Age =
14
,
Name =
'这是一个集合改变'
};


38.
infos[
2
].Name
=
'这是一个属性改变'
;


39.
}


40.


41.
public
class
Students


42.
{


43.
public
int
Id
{ get; set; }


44.
public
string
Name { get; set; }


45.
public
int
Age
{ get; set; }


46.
}


47.
}


48.
}


在这个例子中我们将Students数据对象用ObservableCollection<T>来修饰。这样当我们点击click的时候我们看到。当我们点击后只有student整个对象的改变引发了后台通知机制。





INotifyPropertyChanged实现

INotifyPropertyChanged会向客户端发出某一属性值已更改的通知。当元素属性值改变时,会通知后台model

前台代码不变,我们让后台Students Model实现INotifyPropertyChanged接口。

view
sourceprint?

01.
using
System;


02.
using
System.Collections.Generic;


03.
using
System.Linq;


04.
using
System.Text;


05.
using
System.Windows;


06.
using
System.Windows.Controls;


07.
using
System.Windows.Data;


08.
using
System.Windows.Documents;


09.
using
System.Windows.Input;


10.
using
System.Windows.Media;


11.
using
System.Windows.Media.Imaging;


12.
using
System.Windows.Shapes;


13.
using
System.Collections.ObjectModel;


14.
using
System.ComponentModel;


15.


16.
namespace
WpfApplication1


17.
{


18.
public
partial
class
WindowObservable
: Window


19.
{


20.
ObservableCollection<Students>
infos =
new
ObservableCollection<Students>()
{


21.
new
Students(){
Id=
1
,
Age=
11
,
Name=
'Tom'
},


22.
new
Students(){
Id=
2
,
Age=
12
,
Name=
'Darren'
},


23.
new
Students(){
Id=
3
,
Age=
13
,
Name=
'Jacky'
},


24.
new
Students(){
Id=
4
,
Age=
14
,
Name=
'Andy'
}


25.
};


26.


27.
public
WindowObservable()


28.
{


29.
InitializeComponent();


30.


31.
this
.lbStudent.ItemsSource
= infos;


32.


33.
this
.txtStudentId.SetBinding(TextBox.TextProperty,
new
Binding(
'SelectedItem.Id'
)
{ Source = lbStudent });


34.
}


35.
private
void
button1_Click(object
sender,RoutedEventArgs e)


36.
{


37.
infos[
1
]
=
new
Students()
{ Id =
4
,
Age =
14
,
Name =
'这是一个集合改变'
};


38.
infos[
2
].Name
=
'这是一个属性改变'
;


39.
}


40.
public
class
Students
: INotifyPropertyChanged


41.
{


42.
string
_name;


43.
public
int
Id
{ get; set; }


44.
public
string
Name


45.
{


46.
get
{
return
_name;
}


47.
set
{ _name = value; OnPropertyChanged(
'Name'
);
}


48.
}


49.
public
int
Age
{ get; set; }


50.
protected
internal
virtual
void
OnPropertyChanged(string
propertyName)


51.
{


52.
if
(PropertyChanged
!=
null
)


53.
PropertyChanged(
this
,
new
PropertyChangedEventArgs(propertyName));


54.
}


55.
public
event
PropertyChangedEventHandler PropertyChanged;


56.
}


57.
}


58.
}


此时我们再 运行代码会发现



不管是集合还是对象都发生了改变。至此。我们的整个后台通知就能完美监视任何对象变动。

代码参考:http://blog.csdn.net/fwj380891124/article/details/8194190

本文地址:http://www.cnblogs.com/santian/p/4366832.html

博客地址:http://www.cnblogs.com/santian/
转载请以超链接形式标明文章原始出处。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: