WPF ICollectionView Filter Reset

时光总嘲笑我的痴心妄想 提交于 2019-12-13 06:33:12

问题


I have a CollectionView derived from an ObservableCollection:

private static ObservableCollection<CalculationViewModel> _calculations;

CalculationViewModelsCollection = (CollectionView)CollectionViewSource.GetDefaultView(_calculations);

My problem is that, when the result of the filter is nothing, I'd like to clear the filter, and re-filter with other conditions, but the CollectionView is always empty.

I tried to reset the filter these ways:

CalculationViewModelsCollection.Filter = null;
CalculationViewModelsCollection.Refresh();

and

CalculationViewModelsCollection.Filter = delegate(object p)
{
    return true;
};

But none of them worked.

Could you give some advice how to reset a filter on a CollectionView?


回答1:


From your example, I'm not entirely sure how you're getting your CollectionView, nor am I sure I understand your question correctly.

But anyway, I hope the sample code below helps you with your problem. It's an app that has a listbox containing strings, and a "filter" textbox. if nothing in the list matches the filter, the filter will be set to null and thus display all items.

XAML:

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:v="clr-namespace:WpfApplication3"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <TextBox x:Name="textBox" TextChanged="TextBox_TextChanged"/>        
        <ListBox x:Name="listBox"/>
    </StackPanel>
</Window>

Code-behind:

public partial class MainWindow : Window
{
    ListCollectionView lcv;
    Predicate<object> filterFx;

    public MainWindow()
    {
        InitializeComponent();

        ObservableCollection<string> s = new ObservableCollection<string>();
        "The Quick Brown Fox Jumps Over The Lazy Dog"
            .Split(' ')
            .ToList()
            .ForEach((word) => s.Add(word.ToString()));

        this.lcv = new ListCollectionView(s);
        this.listBox.ItemsSource = this.lcv;

        this.filterFx = (p) => ((string)p).ToUpper().Contains(this.textBox.Text.ToUpper());
        lcv.Filter = this.filterFx;
    }

    private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        lcv.Refresh();

        if (lcv.Count == 0)
            lcv.Filter = null;
        else
            lcv.Filter = filterFx;
    }
}



回答2:


I did a big binding mistake. I don't understand how it works at all.

So the matter is that, it's simple to reset a filter, just set the value to null.

There is one more thing. I tried to create ListCollectionView like you did it.

this.lcv = new ListCollectionView(s);

But the filter didn't work, and I couldn't add SortDescription to the CollectionView.

I create CollectionView this way:

this.lcv = (CollectionView)CollectionViewSource.GetDefaultView(s);

and everything work fine. But ideally your technique have to work too.



来源:https://stackoverflow.com/questions/3520653/wpf-icollectionview-filter-reset

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