关于C#中的ObservableCollection [英] regarding ObservableCollection in c#

查看:111
本文介绍了关于C#中的ObservableCollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了一段使用ObservableCollection的代码,但是他们可以使用list或任何其他与集合相关的类.谁能告诉我使用ObservableCollection的好处.

ObservableCollection<Employee> empData = new ObservableCollection<Employee> 
        {
            new Employee{Name="Diptimaya Patra", Contact="0000", 
                EmailID="diptimaya.patra@some.com", Country="India"},
            new Employee{Name="Dhananjay Kumar", Contact="00020", 
                EmailID="dhananjay.kumar@some.com", Country="India"},
            new Employee{Name="David Paul", Contact="1230", 
                EmailID="david.paul@some.com", Country="India"},
            new Employee{Name="Christina Joy", Contact="1980", 
                EmailID="christina.joy@some.com", Country="UK"},
            new Employee{Name="Hiro Nakamura", Contact="0000", 
                EmailID="hiro.nakamura@some.com", Country="Japan"},
            new Employee{Name="Angela Patrelli", Contact="0000", 
                EmailID="angela.patrelli@some.com", Country="Japan"},
            new Employee{Name="Zoran White", Contact="0000", 
                EmailID="diptimaya.patra@some.com", Country="Scotland"},
        };

请详细讨论.谢谢

解决方案

ObservableCollection实现INotifyPropertyChanged.该界面公开了一些事件,这些事件使您可以在集合的内容发生更改时通知您的集合的使用者.

这主要在WPF中绑定时使用,例如,假设我们有一个ObservableCollection<string>:

ObservableCollection<string> MyStrings
{
    get
    {
        // return a collection with some strings here
    }
}

以及XAML中的此控件:

<ComboBox ItemsSource="{Binding MyStrings}" />

ComboBox将显示ObservableCollection中的字符串.到目前为止,使用List<string>也可以正常工作.但是,如果现在将一些字符串添加到集合中,例如:

<Button Click="AddSomeStrings" Content="Click me!" />

private void AddSomeStrings(object sender, RoutedEventArgs e)
{
    this.MyStrings.Add("Additional string!");
}

您将看到ComboBox的内容将立即更新,并且该字符串将添加到选项列表中.这全部通过INotifyCollectionChanged完成.

i found a piece of code that use ObservableCollection but they can use list or any other collection related classes. can anyone tell me what is the benifit of using ObservableCollection.

ObservableCollection<Employee> empData = new ObservableCollection<Employee> 
        {
            new Employee{Name="Diptimaya Patra", Contact="0000", 
                EmailID="diptimaya.patra@some.com", Country="India"},
            new Employee{Name="Dhananjay Kumar", Contact="00020", 
                EmailID="dhananjay.kumar@some.com", Country="India"},
            new Employee{Name="David Paul", Contact="1230", 
                EmailID="david.paul@some.com", Country="India"},
            new Employee{Name="Christina Joy", Contact="1980", 
                EmailID="christina.joy@some.com", Country="UK"},
            new Employee{Name="Hiro Nakamura", Contact="0000", 
                EmailID="hiro.nakamura@some.com", Country="Japan"},
            new Employee{Name="Angela Patrelli", Contact="0000", 
                EmailID="angela.patrelli@some.com", Country="Japan"},
            new Employee{Name="Zoran White", Contact="0000", 
                EmailID="diptimaya.patra@some.com", Country="Scotland"},
        };

please discuss in detail. thanks

解决方案

ObservableCollection implements INotifyPropertyChanged. This interface exposes events that allow consumers of your collection to be notified when the contents of the collection change.

This is mainly used when binding in WPF, for example let's say we have an ObservableCollection<string>:

ObservableCollection<string> MyStrings
{
    get
    {
        // return a collection with some strings here
    }
}

and this control in XAML:

<ComboBox ItemsSource="{Binding MyStrings}" />

The ComboBox will show the strings inside your ObservableCollection. So far, this would have worked just fine with a List<string> as well. However, if you now add some strings to the collection, for example:

<Button Click="AddSomeStrings" Content="Click me!" />

private void AddSomeStrings(object sender, RoutedEventArgs e)
{
    this.MyStrings.Add("Additional string!");
}

you will see that the contents of the ComboBox will be immediately updated and the string will be added to the list of options. This is all accomplished using INotifyCollectionChanged.

这篇关于关于C#中的ObservableCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆