WPF - 级联组合框,依赖组合框不更新

发布于 2024-10-05 14:56:22 字数 2263 浏览 1 评论 0原文

我有三个 ComboBox,其中 C 的项目列表依赖于 B 中的所选项目,B 的项目列表依赖于 A 中的所选项目。我有 ObservableCollectionItemsSource 特定类 在每个 ComboBox 上,我希望将所选项目的 Name 属性推送到另一个类中的属性。例如:

ObservableCollection<AClass> Items
=> ItemsSource of cbo_A ComboBox

并且 selectedInstanceOfAClass.Name 应推送到 Data.AClassName 属性。我遇到的问题是,当我第一次在 A ComboBox 中选择一个值时,B ComboBox 根据 A 中选定的项目获取适当的项目,如预期的那样。当我第一次在 B 中选择一个项目时也是如此——C 得到了正确的项目。但是,当我在 A 中选择不同的值时,B 中的项目会更新,但 B 的选定值保持不变,并且当我尝试从 B 的新项目中选择新值时,选择不会改变 - -它保持与我最初在 B 中选择的值相同的值。

这是我现在拥有的 XAML:

<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}" />

是什么导致即使我显式单击 ComboBox 并尝试更改值,所选值也不会更新的奇怪行为?

编辑:调试时,我在 cbo_A 上有一个 SelectionChanged 处理程序,在 cbo_B 上有另一个处理程序,我输入了 cbo_B 处理程序在 cbo_A 处理程序之前,因此从 B 的角度来看,所选项目仍然是旧项目,因为 A 尚未更新。 :/

编辑 2: 如果我在 XAML 中翻转 ComboBox 的顺序,建议为 这个问题,这样C先于B先于A,我首先进入A的处理程序。然后,当我输入 cbo_B 的处理程序时,SelectedItem 属性显示旧值(在我在 cbo_A 中选择新值之前先前选择的值) ,即使我单击了组合框中显示的新项目列表中的一个值。

编辑3:我正在尝试这个答案并遇到同样的问题:更改组合框 A 的选定值后,我在组合框 B 中看到新值,但在设置一次后无法更改 B 中的选定值。我开始觉得我还有什么地方出了问题。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗳卜坏 2024-10-12 14:56:22

哇。因此,不要快速为新类实现一堆 IEquatable Equals 方法,因为您最终可能会做我所做的事情:

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

唉。我通过以下方式使 ComboBox 正常工作:

  • 更正我的 BClass.Equals(BClass other) 方法;
  • 使用这个答案关于拥有另一个视图模型具有不同属性的类,例如 SelectedAItemAItems;并
  • 使用这个答案以不同的顺序放置我的XAML声明,以便cbo_C 位于 cbo_B 之前,cbo_B 位于 cbo_A 之前。

我可能会尝试简化事情,看看哪些部分对于我的组合框是绝对必要的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文