【问题标题】:WPF MVVM and parent-child comboboxWPF MVVM 和父子组合框
【发布时间】:2012-12-05 13:08:31
【问题描述】:

我确定我已经在 StackOverflow 上找到了这个,但我似乎不够聪明,无法再次找到它

我想要做的(在 WPF 中使用 MVVM)是这样的:

cmbSelectedAddressRegion: populated with the list of region
cmbSelectedAddressCities: populated with the list of cities in that region

当用户点击cmbSelectedAddressRegion中的一个地区时,cmbSelectedAddressCities中的项目应该只是该地区的城市

我有一个这样的 XAML

    <ComboBox Name="cmbSelectedAddressRegion" 
SelectedValue="{Binding Path=selectedAddressItemRegion, UpdateSourceTrigger=PropertyChanged}" 
IsEnabled="{Binding Path=selectedAddressIsEnabled}" 
Style="{StaticResource style_flat_ComboBox}"></ComboBox>

    <ComboBox Name="cmbSelectedAddressCities" 
SelectedValue="{Binding Path=selectedAddressIdCities, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="id" 
SelectedValuePath="id" 
ItemsSource="{Binding ElementName=cmbSelectedAddressRegion, Path=SelectedItem.Cities}" IsEnabled="{Binding Path=selectedAddressIsEnabled}" 
Style="{StaticResource style_flat_ComboBox}"></ComboBox>

当我点击 cmbSelectedAddressRegion 中的某个区域时,cmbSelectedAddressCities 已正确填充

我还有一个 VM vmCustomer 有很多 DependencyProperties(其中有 selectedAddressItemRegionselectedAddressIdCities) 当我从主列表(包含客户列表的窗口中的另一个组合框)中选择客户时,我看到cmbSelectedAddressRegion 正确显示了区域,但我在cmbSelectedAddressCities 中看不到任何内容。如果我再次点击cmbSelectedAddressRegioncmbSelectedAddressCities 会被填充,并且当前选定的城市(在vmCustomer 中)被选中

cmbSelectedAddressRegion.itemssource 有界(在文件后面的 bode 中)到 ObservableCollection(of vmAddressRegion) 除了其他 DependencyProperties 之外,每个 vmAddressRegion 都有一个 cities 属性,该属性返回一个 ObservableCollection(of vmAddressCities)

ObservableCollection(of vmAddressRegion) 在创建窗口时填充。同时,对于ObservableCollection(of vmAddressRegion)vmAddressRegion 类型)的每个项目,ObservableCollection(of vmAddressCities) 都会填充相应的项目)

我希望我已经足够清楚了 有什么建议可以解决上述问题(cmbSelectedAddressCities 没有被“填充”)?

感谢您的帮助

【问题讨论】:

  • 你的 ViewModel 不应该有 DependencyProperties。
  • 为什么?也许我错了?我开始使用 MVVM 从这里读取link
  • 你的链接是意大利语的,所以我很多都听不懂(它也在VB中,此时我只是看着它就伤害了我的眼睛),但从我所看到的,他在 ViewModels 中做 INotifyPropertyChanged,这消除了对 DependencyProperties 的需要
  • 回答你的问题为什么?这是因为 DependencyProperties 是一个 WPF 概念,并且您的 ViewModel 应该与视图无关(即不依赖于任何 UI 技术)
  • 对不起,我忘了说链接是意大利语的 好的,那么问题是我的描述。我也在 ViewModel 中使用了 INotifyPropertyChanged

标签: wpf data-binding mvvm combobox


【解决方案1】:

需要小心处理 WPF 组合框。如果 ItemsSource 和 SelectedValue/SelectedItem 以“错误”的顺序更改,绑定很容易混淆,尤其是在您使用 SelectedValue 时。

我的建议是用“SelectedItem”替换“SelectedValue”绑定。绑定表达式可以保持不变(尽管我认为您不需要指定 UpdateSourceTrigger)。

绑定到 SelectedItem 意味着您的 vmCustomer 需要一个 selectedAddressCity 属性而不是 selectedAddressIdCity id(并且您可以删除 cmbSelectedAddressCities 的 SelectedValuePath)。

【讨论】:

  • 感谢您的建议 Sphinxxx。我已经做了同样的事情(甚至属性的名称:selectedAddressCity)。深入挖掘之后,我发现了真正的问题。解释起来有点复杂,我现在没有足够的时间(我在办公室)。我将在本周晚些时候发布详细信息和解决方案(以防有人碰巧遇到同样的问题)。
  • 很高兴听到您的声音,我期待看到您的解决方案!
【解决方案2】:

抱歉,耽搁了这么久。

我知道我已承诺在本周末发回我的解决方案,但我的电脑决定第二天就死机了。 关于我原来的问题,我采用了 2 个“解决方案”。 首先,我删除了数字 ID,将其更改为地区/城市的完整描述。我认为这很好,但对于一些“奇怪”的情况(例如第一次单击 Region 组合框时)问题仍然存在。 在代码中挖掘了一段时间后,我发现真正的问题出在我为应用程序编写的转换器中。在转换过程中我犯了一个错误:我写了“if not isnothing”而不是“if isnothing”,结果是一个无转换为空格,搞砸了整个父亲-孩子的关系。 正如我所担心的,问题出在我的代码中,而不是我在此处发布的代码中。

再次感谢大家,再次为延误道歉

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-30
    • 2012-02-29
    • 2018-05-03
    • 2017-02-02
    • 2015-09-15
    • 1970-01-01
    相关资源
    最近更新 更多