2个组合框之间的WPF主数据/明细数据绑定 [英] WPF Master/Detail data binding between 2 combobox

查看:61
本文介绍了2个组合框之间的WPF主数据/明细数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个组合框,其中父级必须显示国家/地区列表,而子级组合必须显示所选国家/地区的城市列表。
数据存储在名称为 CountriesCitiesList Dictionary< Int32,List< String>> 中>。
我有以下代码

I have two combobox where Parent has to show the list of Countries and the child combo has to show a list of cities of the choosen country. The data is stored in a Dictionary<Int32, List<String>> which has the name CountriesCitiesList. I have the following code

<ComboBox x:Name="cbCountriesList" 
    DataContext="{Binding CountriesCitiesList}"
    IsSynchronizedWithCurrentItem="true">
</ComboBox>

<ComboBox x:Name="cbCitiesList" VirtualizingStackPanel.IsVirtualizing="True"                  
          ItemsSource="{Binding CountriesCitiesList}"
          IsSynchronizedWithCurrentItem="true">
</ComboBox>

问题在于,在城市组合中,我无法显示所选国家/地区的城市列表。在我看来,这是最后一步。

The issue is that in the cities combo i can't show the cities list of the country selected. It feel to me that is missing a final step.

推荐答案

如果您的词典 CountriesCitiesList 包含国家Id作为关键字,并列出作为城市名称,您可以使用纯xaml方式将其绑定,如下所示-

If your dictionary CountriesCitiesList contains country Id as Key and List as cities name, you can bind it in pure xaml way something like this -

<ComboBox x:Name="cbCountriesList"
          ItemsSource="{Binding CountriesCitiesList}"
          IsSynchronizedWithCurrentItem="True">
   <ComboBox.ItemTemplate>
      <DataTemplate>
         <TextBlock Text="{Binding Key}"/>
      </DataTemplate>
   </ComboBox.ItemTemplate>
</ComboBox>
<ComboBox x:Name="cbCitiesList"
          ItemsSource="{Binding SelectedItem.Value, ElementName=cbCountriesList}"
          IsSynchronizedWithCurrentItem="True"/>

我假设您要在cbCountriesList中显示国家/地区ID,因为您已将其与键绑定到字典类型为int的类型。

I am assuming you want to show country Id's in cbCountriesList since you are binding it to the dictionary with key of int type.

这篇关于2个组合框之间的WPF主数据/明细数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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