Filtering CollectionViewSource

一笑奈何 提交于 2019-12-24 00:07:40

问题


I want to make a ComboBox bound to my data, with a filter. For that I've created a TextBox and a ComboBox. In the code behind I read a file and generate objects of class Channel that are stored as items of the ComboBox. Although the compiler throws no error the filtering doesn't work properly. If I write something the data is gone, if I erase, it's back. After trying and trying I've realized that if I started typing "myNamespace.myChannel" (Unico.Canal) the data remained, but don't filter. Strange behaviour, indeed. I suspect that I've put something in wrong place.

(for better understanding I've translated the code, Canal=Channel)

Here is the scheme of my code:

namespace Unico
{
        public partial class ControlesArchivo : UserControl, INotifyPropertyChanged
        {
            public ControlesArchivo()
            {

                InitializeComponent();        
            }

    public ObservableCollection<Channel> myListChannels //with INotifyPropertyChanged implemented. But I think I don't need it.

     private void loadButton_Click(object sender, RoutedEventArgs e)
            {

              File loadedFile = new File();
              loadedFile.read(); //Generates a bunch of data in lists.

              foreach (Channel mychan in loadedFile.channels) //Just duplicating the data (maybe this can be avoided)
                   {
                    myListChannels.Add(mychan);
                   }

         var view = CollectionViewSource.GetDefaultView(this.miListaDeCanales);
                        view.Filter = delegate(object o)
                        {
                            if (o.ToString().Contains(myTextBox.Text)) //Delicate place
                            {
                                return true;
                            }
                            return false;
                        };

                myComboBox.ItemsSource = view;
     DataContext = this;
    }


     private void myTextBox_TextChanged(object sender, TextChangedEventArgs e)
            {


                       ((ICollectionView)myComboBox.ItemsSource).Refresh();
                       myComboBox.SelectedIndex = 0;

            }


      }
    }

The data is bound in XAML with:

 ItemsSource="{Binding view}" 

EDIT: I think I know where is the problem: I'm not specifing the property to filter. I mean, what you see in the ComboBox is the property channelName of the class Channel listed in myListChannels. When I'm setting the filter, shouldn't I let know what I'm filtering? How could I write this? Thank you very much.


回答1:


Yes your assumption is correct.

I'm assuming with your translations,

public ObservableCollection<Channel> myListChannels;

is actually

public ObservableCollection<Canal> miListaDeCanales;

with the class Canal in the namespace Unico

Update:

In your filter try using the property that is rendered in the ComboBox than use the ToString() on the object(o) if you've not overridden ToString() from System.Object.

try switching

if (o.ToString().Contains(myTextBox.Text))

to

if (((Canal)o).NameProperty.Contains(myTextBox.Text))

^^ that should fix your issue.

Do you have a DataTemplate for ComboBox.ItemTemplate in xaml. That will explain why you see the valid value rendered in the ComboBox, else all the ComboBoxItem's will also render as Unico.Canal



来源:https://stackoverflow.com/questions/16816983/filtering-collectionviewsource

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