【发布时间】: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(其中有 selectedAddressItemRegion 和 selectedAddressIdCities)
当我从主列表(包含客户列表的窗口中的另一个组合框)中选择客户时,我看到cmbSelectedAddressRegion 正确显示了区域,但我在cmbSelectedAddressCities 中看不到任何内容。如果我再次点击cmbSelectedAddressRegion,cmbSelectedAddressCities 会被填充,并且当前选定的城市(在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