views:

241

answers:

1

Hello, I will try and explain this as concise as possible. I have 2 objects, the first which we will call object A that has an Id property and the second we will call object B, which has a ParentId property. The obvious relationship is that object B's ParentId is set to an object A's Id property. I am using the MVVM pattern, so on the viewmodel I have 2 ObservableCollections, one containing objects A the other objects B. On construction of the viewmodel, I create and fill the ObservableCollection<'A'> named ListItems. My xaml is simple,

<StackPanel>
    <ListBox IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding ListItems}">
    </ListBox>

    <ComboBox SelectedValuePath="ParentId" SelectedValue="{Binding Path=ListItems/Id, Mode=OneWay}" ItemsSource="{Binding ComboItems}">
    </ComboBox>

    <Button Click="Button_Click" Content="Push Me"/>            
</StackPanel>

As you can see the combobox's SelectedValue is bound to the ListItems current item's Id property. So essentially the listbox and combobox are in a master details.

If you press the button, it will fill the ObservableCollection<'B'> name ComboItems, which in turn populates the combobox. Now here is where the oddity begins. When I start the program, if the only thing I do is press the button, and then afterwords select an item in the listbox, the combobox will properly select an item due to the SelectedValue binding. But if I start the program and first select an item in the listbox and then press the button, the current combobox item will not change with the current listbox item. The binding appears to be forever broken. Does anyone know why this happens?

Ps. If I set the ItemsSource on the combobox before I set the SelectedValue/SelectedValuePath, the master/detail binding will never work. I know there is order to xaml, but that seems a little fragile. So if anyone has input on that also, I am all ears.

Thanks, Nate

EDIT -

When binding SelectedValue, it is very fragile. If the binding is working, i.e. have not selected anything in the listbox and then filled the combobox, if you choose an item in the combobox, the binding will break. After much time wasted with this, I chose to bind SelectedItem. This binding does not break in any of the conditions I have previously specified. I would however take any answers as to why SelectedValue binding is so ridiculous. Thanks again to all that have answered or will answer.

+1  A: 

Yeah this is a problem we stumble upon quite a lot.

The problem is that after the ItemsSource property gets a new value, the SelectedValue binding will be cleared. Sucks, and until today we have not found a proper solution.

Here are a few workarounds:

  • Reset the SelectedValue binding in code, as soon as the new ItemsSource has been set. You can do this in a converter, or somewhere you'll know which will replace the ItemsSource binding (like the DataContextChanged event).

  • Instead of using the Binding on ItemsSource, try using a CollectionViewSource and a Filter. Put all your items in the CollectionViewSource object and filter the items when your combobox changes value.

  • Manually get your item the old fashion way when your listbox throws a SelectionChanged event.

Mind you, all solutions are not the prettiest in the book. I would go for option 2, its the cleanest IMO ;)

Hope this helps!

Arcturus
Thanks for your answer. I see how this applies to the order in which you hook your bindings in xaml, but I am not sure it applies to my first question, the one pertaining to selecting an item in the listbox before filling the combobox. In that scenario, the ObservableCollection the combobox is bound to, is never replaced but only filled with data, so I don't understand/think that the SelectedValue binding would be cleared. Thank you for answering my second question though. It seems odd design, but I guess owell.
NastyNateDoggy
That seems odd indeed. Perhaps your ListItems collection is somehow affected. You do seem to have IsSynchronizedWithCurrentItem set to true, which means it will match the CurrentItem of your collection to the SelectedItem which initially will be null. Perhaps this will reset your selected item in your ListBox?
Arcturus
I marked this as the answer even though I feel it doesn't fully answer my question. Read my edit above to see my final solution.
NastyNateDoggy
Thanks.. I will favorite your question, because we are also still looking for a better solution/explanation to this problem.
Arcturus