加入收藏 | 设为首页 | 会员中心 | 我要投稿 东莞站长网 (https://www.0769zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 编程要点 > 语言 > 正文

分析C# ObservableCollection和List

发布时间:2021-06-08 02:00:21 所属栏目:语言 来源:互联网
导读:一、ObservableCollection和List的区别 1)ObservableCollection比较简单,继承了Collection, INotifyCollectionChanged, INotifyPropertyChanged Collection:为

一、ObservableCollection和List的区别

1)ObservableCollection比较简单,继承了Collection, INotifyCollectionChanged, INotifyPropertyChanged

Collection:为泛型集合提供基类。

INotifyCollectionChanged:将集合的动态更改通知给侦听器,例如,何时添加和移除项或者重置整个集合对象。

INotifyPropertyChanged:向客户端发出某一属性值已更改的通知。

所以再ObservableCollection这个类的方法,对数据的操作很少,重点放在了当自己本事变化的时候(不管是属性,还是集合)会调用发出通知的事件。(一般用于更新UI,当然也可以用于写其他的事情。这个以后会写)

2)List就比较多了,继承了IList, ICollection, IEnumerable, IList, ICollection, IEnumerable。

IList:表示可按照索引单独访问的一组对象。

ICollection:定义操作泛型集合的方法。

IEnumerable:公开枚举器,该枚举器支持在指定类型的集合上进行简单迭代。

IList:表示可按照索引单独访问的对象的非泛型集合。

ICollection:定义所有非泛型集合的大小、枚举器和同步方法。

IEnumerable:公开枚举器,该枚举器支持在非泛型集合上进行简单迭代。

二、举例:

1、举例1:

MainWindow.xaml:

<ListBox x:Name="listbind" Height="61" HorizontalAlignment="Left" Margin="146,12,0,0" VerticalAlignment="Top" Width="120" >   

<ListBox.ItemTemplate>   

<DataTemplate>   

<TextBlock Text="{Binding Name}" />   

</DataTemplate>   

</ListBox.ItemTemplate>   

</ListBox>   

<ListBox x:Name="observbind" Height="74" HorizontalAlignment="Left" Margin="146,111,0,0" VerticalAlignment="Top" Width="120" >   

<ListBox.ItemTemplate>   

<DataTemplate>   

<TextBlock Text="{Binding Name}" />   

</DataTemplate>   

</ListBox.ItemTemplate>   

</ListBox>   

<TextBlock Height="23" HorizontalAlignment="Left" Margin="38,58,0,0" Name="textBlock1" Text="List绑定数据" VerticalAlignment="Top" />   

<TextBlock Height="44" HorizontalAlignment="Left" Margin="12,125,0,0" Name="textBlock2" Text="ObservableCollection绑定数据" VerticalAlignment="Top" Width="112" />   

<Button Content="Button" Height="23" HorizontalAlignment="Left"  

xaml页面很简单,托2个listbox分别用来绑定ObservableCollection和List

Person.cs:

public class Person   

     { 

         public string Name { get; set; } 

     } 

MainWindow.xaml.cs:

private List<Person> person1 = new List<Person>(); 

private ObservableCollection<Person> person2 = new ObservableCollection<Person>(); 

public DemoTestDiff()   

InitializeComponent(); 

person1.Add(new Person() { Name = "张三" }); 

person1.Add(new Person() { Name = "李四" }); 

listbind.ItemsSource = person1; 

person2.Add(new Person() { Name = "张三" }); 

person2.Add(new Person() { Name = "李四" }); 

observbind.ItemsSource = person2; 

private void button1_Click(object sender, RoutedEventArgs e)   

person1.Add(new Person() { Name = "王五" }); 

person2.Add(new Person() { Name = "王五" }); 

运行程序点击button按钮,然后只有ObservableCollection的有添加。 

表示当集合对象的集合改变时,只有ObservableCollection会发出通知更新UI。

这只是他们两个区别之一。

2、举例2

以下方法可以更新ListView的UI:

private ObservableCollection<PreviewListModel> _previewList = new ObservableCollection<PreviewListModel>(); 

/// <summary> 

/// 预览信息列表 

/// </summary> 

public ObservableCollection<PreviewListModel> PreviewList 

get { return _previewList; } 

set { SetProperty(ref _previewList, value); } 

//set { _previewList = value; RaisePropertyChanged("PreviewList"); } 

(编辑:东莞站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读