Question

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.

enter image description hereenter image description hereenter image description here

(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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top