加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

说明C# ObservableCollection和List

发布时间:2021-06-02 20:41:47 所属栏目:编程 来源:互联网
导读:一、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"); } 

(编辑:湖南网)

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

    热点阅读