I'm sure I already found this on *, but I don't seem to be smart enough to find it again
我确定我已经在*上找到了这个,但我似乎不够聪明,无法再找到它
What I want to do (in WPF using MVVM) is this:
我想做的(在使用MVVM的WPF中)是这样的:
cmbSelectedAddressRegion: populated with the list of region
cmbSelectedAddressCities: populated with the list of cities in that region
When the user click on a region in cmbSelectedAddressRegion
the items in cmbSelectedAddressCities
should be the cities of that region only
当用户单击cmbSelectedAddressRegion中的某个区域时,cmbSelectedAddressCities中的项目应仅为该区域的城市
I have an XAML like this
我有这样的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>
When I click on a region in cmbSelectedAddressRegion the cmbSelectedAddressCities is correctly populated
当我单击cmbSelectedAddressRegion中的某个区域时,会正确填充cmbSelectedAddressCities
I also have a VM vmCustomer
with a lot of DependencyProperties
(amongst them selectedAddressItemRegion
and selectedAddressIdCities
) When I select the customer from the master list (another combobox in the window which holds the list of customers) I see the cmbSelectedAddressRegion
correctly showing the Region, but I don't see anything in the cmbSelectedAddressCities
. Again if I click on the cmbSelectedAddressRegion
the cmbSelectedAddressCities
is populated and the currently selected cities (in the vmCustomer
) is selected
我还有一个带有很多DependencyProperties的VM vmCustomer(其中包括selectedAddressItemRegion和selectedAddressIdCities)当我从主列表中选择客户时(窗口中另一个组合框包含客户列表)我看到cmbSelectedAddressRegion正确显示了Region,但是我在cmbSelectedAddressCities中没有看到任何内容。再次,如果我单击cmbSelectedAddressRegion,将填充cmbSelectedAddressCities并选择当前选定的城市(在vmCustomer中)
The cmbSelectedAddressRegion.itemssource
is bounded (in bode behind file) to an ObservableCollection(of vmAddressRegion)
Each vmAddressRegion
has, amongst other DependencyProperties, a cities
properties which returns an ObservableCollection(of vmAddressCities)
cmbSelectedAddressRegion.itemssource被绑定(在文件后面的bode中)到一个ObservableCollection(vmAddressRegion)每个vmAddressRegion在其他DependencyProperties中都有一个返回ObservableCollection(of vmAddressCities)的cities属性。
The ObservableCollection(of vmAddressRegion)
is populated when the window is created. At the same time, for every item of ObservableCollection(of vmAddressRegion)
(of type vmAddressRegion
) the ObservableCollection(of vmAddressCities)
is populated with the corresponding items)
创建窗口时将填充ObservableCollection(of vmAddressRegion)。同时,对于ObservableCollection(vmAddressRegion)(类型为vmAddressRegion)的每个项目,ObservableCollection(vmAddressCities)都填充了相应的项目)
I hope I've been clear enough Any suggestion how to resolve the problem above (the cmbSelectedAddressCities
not being "populated")?
我希望我已经足够清楚任何建议如何解决上面的问题(cmbSelectedAddressCities没有被“填充”)?
Thanks for any help
谢谢你的帮助
2 个解决方案
#1
0
The WPF ComboBox needs to be handled with care. Bindings easily get confused if ItemsSource and SelectedValue/SelectedItem change in the "wrong" order, especially if you use SelectedValue.
需要小心处理WPF ComboBox。如果ItemsSource和SelectedValue / SelectedItem以“错误”顺序更改,则绑定很容易混淆,特别是如果您使用SelectedValue。
My advice would be to replace the "SelectedValue" bindings with "SelectedItem". The binding expressions can stay the same (although I don't think you need to specify the UpdateSourceTrigger).
我的建议是用“SelectedItem”替换“SelectedValue”绑定。绑定表达式可以保持不变(尽管我认为您不需要指定UpdateSourceTrigger)。
Binding to SelectedItem means that your vmCustomer needs a selectedAddressCity property instead of the selectedAddressIdCity id (and you can remove cmbSelectedAddressCities' SelectedValuePath).
绑定到SelectedItem意味着您的vmCustomer需要selectedAddressCity属性而不是selectedAddressIdCity id(并且您可以删除cmbSelectedAddressCities的SelectedValuePath)。
#2
0
Sorry for the long delay.
对不起,拖延很长时间。
I know I promised to post back my solution during the end of the week, but my pc decided to die the following day. Regarding my original problem I adopted 2 "solutions". First I removed the numeric ID changing it to the full description of the Region/City. I thought this was good, but the problem still remained for some "strange" cases (for example when clicking the first time on the Region combobox. After digging a while in the code I discovered the real problem was in a converter I wrote for the application. During a conversion I made a mistake: instead of "if isnothing" I wrote "if not isnothing" and the resulting was a nothing converted to space, messing up the whole father-child relation. As I was worried, the problem was in my code, and not in the piece of code I posted here.
我知道我答应在本周末回复我的解决方案,但我的电脑决定第二天就死了。关于我原来的问题,我采用了2个“解决方案”。首先,我删除了数字ID,将其更改为Region / City的完整描述。我认为这很好,但问题仍然存在于某些“奇怪”的情况下(例如,当第一次点击Region组合框时。在代码中挖了一段时间后我发现真正的问题出在我为其编写的转换器中在转换过程中,我犯了一个错误:不是“如果没有”我写的“如果不是没有”,结果就是没有任何东西转化为空间,弄乱了整个父子关系。我担心,问题是在我的代码中,而不是我在这里发布的代码片段。
I thanks again everyone, and apologize again for the delay
我再次感谢大家,并再次为延误道歉
#1
0
The WPF ComboBox needs to be handled with care. Bindings easily get confused if ItemsSource and SelectedValue/SelectedItem change in the "wrong" order, especially if you use SelectedValue.
需要小心处理WPF ComboBox。如果ItemsSource和SelectedValue / SelectedItem以“错误”顺序更改,则绑定很容易混淆,特别是如果您使用SelectedValue。
My advice would be to replace the "SelectedValue" bindings with "SelectedItem". The binding expressions can stay the same (although I don't think you need to specify the UpdateSourceTrigger).
我的建议是用“SelectedItem”替换“SelectedValue”绑定。绑定表达式可以保持不变(尽管我认为您不需要指定UpdateSourceTrigger)。
Binding to SelectedItem means that your vmCustomer needs a selectedAddressCity property instead of the selectedAddressIdCity id (and you can remove cmbSelectedAddressCities' SelectedValuePath).
绑定到SelectedItem意味着您的vmCustomer需要selectedAddressCity属性而不是selectedAddressIdCity id(并且您可以删除cmbSelectedAddressCities的SelectedValuePath)。
#2
0
Sorry for the long delay.
对不起,拖延很长时间。
I know I promised to post back my solution during the end of the week, but my pc decided to die the following day. Regarding my original problem I adopted 2 "solutions". First I removed the numeric ID changing it to the full description of the Region/City. I thought this was good, but the problem still remained for some "strange" cases (for example when clicking the first time on the Region combobox. After digging a while in the code I discovered the real problem was in a converter I wrote for the application. During a conversion I made a mistake: instead of "if isnothing" I wrote "if not isnothing" and the resulting was a nothing converted to space, messing up the whole father-child relation. As I was worried, the problem was in my code, and not in the piece of code I posted here.
我知道我答应在本周末回复我的解决方案,但我的电脑决定第二天就死了。关于我原来的问题,我采用了2个“解决方案”。首先,我删除了数字ID,将其更改为Region / City的完整描述。我认为这很好,但问题仍然存在于某些“奇怪”的情况下(例如,当第一次点击Region组合框时。在代码中挖了一段时间后我发现真正的问题出在我为其编写的转换器中在转换过程中,我犯了一个错误:不是“如果没有”我写的“如果不是没有”,结果就是没有任何东西转化为空间,弄乱了整个父子关系。我担心,问题是在我的代码中,而不是我在这里发布的代码片段。
I thanks again everyone, and apologize again for the delay
我再次感谢大家,并再次为延误道歉