WPF Master/Detail data binding between 2 combobox

后端 未结 3 1518
太阳男子
太阳男子 2021-01-28 05:48

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 Diction

相关标签:
3条回答
  • 2021-01-28 06:29

    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"/>
    

    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.

    0 讨论(0)
  • 2021-01-28 06:33

    For the parent ComboBox, bind the SelectedItem to a property on your model:

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

    Where SomePropertyOnModel is of the same type as an Item in the Countries List.

    For the child ComboBox, everything should be the same:

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

    Side Note: You'll notice that I specifically added the ItemsSource binding to both the ComboBoxes.

    In the model, whenever the SomePropertyOnModel is set, update the CountriesCitiesList based on the value received, i.e.,:

    private string _somePropertyOnModel;
    public string SomePropertyOnModel 
    {
        get { return _somePropertyOnModel; }
        set 
        {
            _somePropertyOnModel = value;
            // call NotifyPropertyChanged
            UpdateCountriesCitiesList();
        }
    }
    
    private void UpdateCountriesCitiesList()
    {
        // set CountriesCitiesList based on the 
        // SomePropertyOnModel value
        // CountriesCitiesList should be an ObservableCollection and the values
        // should be cleared and then added.
        CountriesCitiesList.Clear();
        CountriesCitiesList.Add( "Springfield" );
    }
    
    0 讨论(0)
  • 2021-01-28 06:33

    ----------------------------MASTER-------------------------

    <ComboBox 
        x:Name="CityCB" 
        ItemsSource="{Binding CitiesList}" 
        DisplayMemberPath="CITYNAME" 
        IsSynchronizedWithCurrentItem="True"/>
    

    ----------------------------DETAIL-------------------------

    <ComboBox 
        x:Name="RegionCB" 
        ItemsSource="{Binding SelectedItem.REGIONS, ElementName=CityCB}"
        DisplayMemberPath="REGIONNAME" 
        IsSynchronizedWithCurrentItem="True"/>
    

    In this example i'm illustrating master-detail between City and the Regions which it contains.

    0 讨论(0)
提交回复
热议问题