简体   繁体   中英

StringCollection vs. ObservableCollection<string>

I am currently trying to grasp the concepts of Collections in C#. I am working on an App that is based on WPF and MVVM. I want my ViewModel to have some sort of List or Collection of strings, which can make use of an "OnChangedEventHandler", such as the NotifyCollectionChangedEventHandler . This would be provided by the ObservableCollection class. In contrast to the ObservableCollection class, the StringCollection class prvides a Contains method to determine whether the specified string is in the Collection.

What should I use if I want to have both the "Contains method" and the "OnChangedEventHandler"?

UPDATE

Solved by:

public class MyStringCollection : ObservableCollection<string>
{
    private ObservableCollection<string> strings = new ObservableCollection<string>();

    // Check if MyStringCollection contains the specified string
    public bool Contains(string str)
    {
        return (strings.Any(c => (String.Compare(str, c) == 0)));
    }        
}

ObservableCollection supports the Conatins method extension.

Just add the following using statement to your class:

using System.Linq;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM