WPF - cascading ComboBoxes, dependent ones don't update

I have three ComboBoxes such that C's items list is dependent on the selected item in B and B's items list is dependent on the selected item in A. I have ObservableCollections of particular classes for the ItemsSource on each ComboBox, and I want the Name property of the selected item to be pushed to a property in another class. For example:

ObservableCollection<AClass> Items
=> ItemsSource of cbo_A ComboBox

And selectedInstanceOfAClass.Name should be pushed to Data.AClassName property. The problem I have is that when I choose a value in the A ComboBox the first time, the B ComboBox gets the appropriate items based on the selected item in A, as expected. Same for when I select an item in B for the first time--C gets the right items. However, when I choose a different value in A, the items in B get updated but the selected value of B stays the same, and when I try to select a new value in B from its new items, the selection doesn't change--it stays the same selected value as what I initially selected in B.

Here's the XAML I have right now:

<ComboBox x:Name="cbo_A"
    ItemsSource="{Binding Path=Lists.AItems, Mode=OneWay}"
    DisplayMemberPath="Name" SelectedValuePath="Name"
    SelectedValue="{Binding Path=Data.AClassName, ValidatesOnDataErrors=True,
        Mode=OneWayToSource}" />

<ComboBox x:Name="cbo_B"
    ItemsSource="{Binding ElementName=cbo_A, Path=SelectedItem.BItems, Mode=OneWay}"
    SelectedValuePath="Name" DisplayMemberPath="Name"
    SelectedValue="{Binding Path=Data.BClassName, ValidatesOnDataErrors=True,
        Mode=OneWayToSource}"/>

<ComboBox x:Name="cbo_C"
    ItemsSource="{Binding ElementName=cbo_B, Path=SelectedItem.CItems, Mode=OneWay}"
    SelectedValuePath="Name" DisplayMemberPath="Name"
    SelectedValue="{Binding Path=Data.CClassName, Mode=OneWayToSource,
        ValidatesOnDataErrors=True}" />

What is causing the weird behavior with the selected value not updating even when I explicitly click the ComboBox and try to change the value?

Edit: when debugging and I had a SelectionChanged handler on cbo_A and another on cbo_B, I entered the cbo_B handler before the cbo_A one, so the selected item from B's perspective was still the old item, because A had not been updated yet. :/

Edit 2: if I flip the order of my ComboBoxes in XAML, suggested by this question, such that C comes before B comes before A, I enter A's handler first. When I then enter the handler for cbo_B, the SelectedItem property shows the old value (previously selected, before I chose a new value in cbo_A), even though I clicked a value from the new list of items showing up in the ComboBox.

Edit 3: I'm trying a version of this answer and getting the same problem: I see new values in ComboBox B upon changing the selected value of ComboBox A, but I cannot change the selected value in B after I've already set it once. I'm beginning to think I've got something else going awry.

Answers


Wow. So, don't go quickly implementing a bunch of IEquatable<T> Equals methods for new classes, because you may end up doing what I did:

if (ReferenceEquals(this, other))
{
    return false;  // Say what??
}

Sigh. I got my ComboBoxes to work by:

  • Correcting my BClass.Equals(BClass other) method;
  • Using this answer about having another view model class with different properties like SelectedAItem and AItems; and
  • Using this answer about putting my XAML declarations in a different order so cbo_C comes before cbo_B and cbo_B comes before cbo_A.

I may try simplifying things and see what parts are absolutely necessary for my ComboBoxes.


Need Your Help

Embed a Registration-Free COM manifest into a C# dll with native/managed environment

c# .net dll com-interop regfreecom

I'm currently working on a mixed native / managed application chain, which employs registration-free COM. The following image illustrates this:

Add-on Trigger not running, how can I troubleshoot

google-apps-script google-spreadsheet

My add-on creates a trigger that runs every hour. It works when I run it from the script, but it does not seem to work when published as an add-on. I am wondering what is the best way to trouble...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.