C#WPF组合框过滤器 [英] C# WPF combobox filter

查看:78
本文介绍了C#WPF组合框过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我需要一些帮助,我正在做一个项目,我有一个组合框和一个listView和组合框有两个选项可用于过滤列表视图但是当我选择组合框中的选项来过滤列表视图时,如果listView没有任何东西加载项目崩溃,说没有nullReferencEexception,解决我要显示一个消息框提醒列表列表首先,我试试这个但是如果你可以提供帮助,那就错了这是代码:



我尝试过:



Hello guys i need some help, i´m doing a project where i have a combobox and a listView and the combobox have two options available to filters the listview but when i select the option in the combobox to filter the listview, if the listView don´t have nothing loaded the project crash, saying that there exist a not nullReferencEexception, to solve that i want to show a message box to alert to list the list first,i try this but something is wrong if you can help, here is the code:

What I have tried:

private void comboBox_filtro_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (comboBox_filtro.SelectedItem == null)
            {
               MessageBox.Show("Consultar Primeiro a lista");
            }
            else
            {
                ICollectionView view = CollectionViewSource.GetDefaultView(listView.ItemsSource);
                view.SortDescriptions.Clear();

                if (view.CanFilter)
                {
                    view.Filter = new Predicate<object>(view.Filter = myTextFilter);
                }
            }

        }

推荐答案

如果你只是想停止NullReferenceException(以及一般的清洁度)试试这个:



If you just want to stop the NullReferenceException (And also for general cleanliness) try this:

private void comboBox_filtro_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (comboBox_filtro.SelectedItem == null)
            {
                MessageBox.Show("Consultar Primeiro a lista");
            }
            else
            {
                ICollectionView view = CollectionViewSource.GetDefaultView(listView.ItemsSource);
                if (view != null)
                {
                    view.SortDescriptions.Clear();

                    if (view.CanFilter)
                    {
                        view.Filter = new Predicate<object>(view.Filter = myTextFilter);
                    }
                }
             }
        }





这真的不是你的方式但是,我想做WPF。您真正想要做的是将您的组合框SelectedItem绑定到视图模型上实现INotifyPropertyChanged的对象,以便在所选数据更改时,您的viewmodel可以过滤集合。不过,这确实是最好的做法。您的代码没有理由不起作用,只需抛出一个空检查。如果您使用的是较新版本的.NET,也可以使用null check运算符。



看起来更像是这样:





This really isn't how you want to do WPF, though. What you really want to do is bind your combobox SelectedItem to an object on your view model that implements INotifyPropertyChanged so that when the selected data changes your viewmodel can filter the collection. That is really just best practice, though. There is no reason your code won't work, just throw a null check. You can also use the null check operator if you are using a newer version of .NET.

That would look more like this:

view?.SortDescriptions.Clear()





然后





and then

<pre lang="c#">if (view?.CanFilter)


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

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