WPF - 级联ComboBoxes,依赖的不会更新 [英] WPF - cascading ComboBoxes, dependent ones don't update

查看:187
本文介绍了WPF - 级联ComboBoxes,依赖的不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个ComboBox,这样C的项目列表依赖于B中的所选项目,B的项目列表依赖于A中的所选项目。我有 ObservableCollection s在每个ComboBox上的 ItemsSource 的特定类别,我想要将所选项目的 Name 属性推送到另一类的财产。例如:

  ObservableCollection< AClass>项目
=> 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并尝试更改值?



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



编辑2:如果我在XAML中翻转我的ComboBoxes的顺序,由,这样C来到B之前就是A之前,我先输入A的处理程序。当我进入 cbo_B 的处理程序时, SelectedItem 属性显示旧值(以前选择,在我选择一个即使我从组合框中显示的新项目列表中单击一个值, cbo_A 中的新值



编辑3:我正在尝试一个这个答案,并得到同样的问题:在ComboBox A中更改所选值时,我会看到ComboBox B中的新值,但是在我已经设置之后我无法更改B中的选定值一次我开始认为我有其他的东西不好了。

解决方案

哇。所以,不要快速地为新课程实施一堆 IEquatable< T> 等于方法,因为你可能最终做我做的:

  if(ReferenceEquals(this,other))
{
return假; // 说什么??
}

叹了口气。我有我的组合框工作:




  • 更正我的 BClass.Equals(BClass other)方法;

  • 使用这个答案关于另一个视图模型类具有不同的属性,如 SelectedAItem AItems ;和

  • 使用此答案关于将我的XAML声明放在不同的顺序,所以 cbo_C 来自 cbo_B cbo_B 来自 cbo_A



我可以尝试简化事情,看看我的ComboBox是什么零件绝对必要的。


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.

解决方案

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.

这篇关于WPF - 级联ComboBoxes,依赖的不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆