WPF ICollectionView 筛选器重置

发布于 2024-09-14 20:54:36 字数 703 浏览 3 评论 0原文

我有一个从 ObservableCollection 派生的 CollectionView

private static ObservableCollection<CalculationViewModel> _calculations;

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

我的问题是,当过滤器的结果是什么时,我想清除过滤器,然后重新过滤与其他条件,但 CollectionView 始终为空。

我尝试通过以下方式重置过滤器:

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

但是

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

它们都不起作用。

您能否提供一些如何重置 CollectionView 上的过滤器的建议?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我为君王 2024-09-21 20:54:36

从您的示例来看,我不完全确定您如何获取 CollectionView,也不确定我是否正确理解您的问题。

但无论如何,我希望下面的示例代码可以帮助您解决问题。它是一个具有包含字符串的列表框和“过滤器”文本框的应用程序。如果列表中没有任何内容与过滤器匹配,则过滤器将设置为空,从而显示所有项目。

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>

隐藏代码:

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;
    }
}

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;
    }
}
情绪失控 2024-09-21 20:54:36

我犯了一个很大的绑定错误。我根本不明白它是如何工作的。

所以问题是,重置过滤器很简单,只需将值设置为 null 即可。

还有一件事。我尝试像您一样创建 ListCollectionView 。

this.lcv = new ListCollectionView(s);

但过滤器不起作用,我无法将 SortDescription 添加到 CollectionView 中。

我以这种方式创建 CollectionView:

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

一切正常。但理想情况下,你的技术也必须有效。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文