0

I've looked through a lot of articles about this subject but so far none have suggested a solution that works for me.

I'm working with a FilteredComboBox that I've found in another article on this site (Dynamic filter of WPF combobox based on text input, the fixed version by YantingChen). I'm trying to get it to work with my code.

I have a viewmodel that performs the following code when the view is opened:

Object = _context.ObjectReader.GetObjectById(i); //returns an object with property Prop, that is of type Item
Items = _context.ItemReader.GetAllItems(); //returns an ObservableCollection<Item>. Item has a property Name
RaisePropertyChanged(nameof(Object));
RaisePropertyChanged(nameof(Items));

My xaml uses this for the FilteredComboBox:

<local:FilteredComboBox ItemsSource="{Binding Items}"
                        SelectedItem="{Binding Object.Prop}"
                        DisplayMemberPath="Name"
                        Width="200">
    <local:FilteredComboBox.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel VirtualizationMode="Recycling"/>
        </ItemsPanelTemplate>
    </local:FilteredComboBox.ItemsPanel>
</local:FilteredComboBox>

What I'm trying to accomplish with this is that the combobox shows the current value of Object.Prop when the view is opened, but this does not happen with the current setup. From what I've gathered by googling around for this problem, binding to the SelectedItem of a combobox comes with some odd difficulties, but I'm hoping someone here will be able to point me in the right direction.

I've tried the following:

<local:FilteredComboBox ItemsSource="{Binding Items}"
                        SelectedItem="{Binding Object.Prop}"
                        SelectedValue="{Binding Object.Prop.Name}"
                        DisplayMemberPath="Name"
                        Width="200">
    <local:FilteredComboBox.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel VirtualizationMode="Recycling"/>
        </ItemsPanelTemplate>
    </local:FilteredComboBox.ItemsPanel>
</local:FilteredComboBox>

This almost seemed to work except it's somehow setting the selection to the wrong item in the itemssource.

Bart K
  • 115
  • 1
  • 2
  • 8
  • The object returned from Prop must compare equal to an element from the Items collection. Either override the Equals method of the Items class or use SelectValue and SelectValuePath instead of SelectedItem. Do not use SelectedValue and SelectedItem together. – Clemens Dec 03 '18 at 13:01
  • Selection tracking in lists is usually done via a collection view. If you do not hand the GUI classes one, they will create one from the list you gave them. So usually you need to take over the creation of the CV. – Christopher Dec 03 '18 at 13:06
  • @Clemens, I've tried overriding the Equals method so that it compares the Id properties of Item, and it's being called several times, some of which are returning true. However, the combobox is still empty. – Bart K Dec 03 '18 at 13:21
  • From what we can tell from your code sample, it should compare the Items's Name. Anyway, how about SelectedValue with SelectedValuePath? – Clemens Dec 03 '18 at 13:22
  • That actually does fix the problem, except it seems to have broken the filter and combobox functionality... It's no longer showing a dropdown. Also, VS is complaining that it can't convert the item of type string to type Item. – Bart K Dec 03 '18 at 13:27
  • Looking through the FilteredComboBox class, I think the class might require the SelectedItem to perform its filtering, so I will need to use SelectedItem. – Bart K Dec 03 '18 at 13:39

0 Answers0