Filtering an ObservableCollection?

徘徊边缘 提交于 2019-12-28 06:01:28

问题


When I bind a ListBox directly to an ObservableCollection I get the real-time updates displayed in my ListBox, but as soon as I add other LINQ methods in the mix my ListBox is no longer notified of any changes to the ObservableCollection.

Here, let me illustrate with an example;

public partial class MainPage : PhoneApplicationPage
{
    ObservableCollection<String> Words = new ObservableCollection<string>();

    public MainPage()
    {
        InitializeComponent();
        listBox1.ItemsSource = Words;
    }

    private void AddButton_Click(object sender, RoutedEventArgs e)
    {
        Words.Add(DateTime.Now.ToString());
    }
}

Here I've added a Button and a ListBox to a simple Page, and clicking the button makes the new item appear immediately in the ListBox.

However, if I change from

        listBox1.ItemsSource = Words;

to

        listBox1.ItemsSource = Words.Where(w => w.Contains(":"));

the ListBox is no longer updated.

How can I add a "filter" between my ObservableCollection and the ListBox, and still get it to update without having to set the .ItemsSource again?


回答1:


Try using the CollectionViewSource like this:

WordsView = new CollectionViewSource();
WordsView.Filter += Words_Filter;
WordsView.Source = Words;

// ...
void Words_Filter(object sender, FilterEventArgs e)
{
    if (e.Item != null)
        e.Accepted = ((string)e.Item).Contains(":");
}



回答2:


Why it does not work:

listBox1.ItemsSource = Words.Where(w => w.Contains(":"));

Your are not binding the ObservableCollection but the IEnumerable generated by Linq. This new "list" does not notify the ListBox about changes in the list.




回答3:


You should use the ICollectionView.Filter property:

ICollectionView view = CollectionViewSource.GetDefaultView(Words);
view.Filter = WordFilter;

...


bool WordFilter(object o)
{
    string w = (string)o;
    return w.Contains(":")
}


来源:https://stackoverflow.com/questions/5782585/filtering-an-observablecollection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!