WPF binding to combobox

I have a list of tabs where Tab is:

public class Tab
{
  public int Id {get; set;}
  public string Name {get; set}
  public List<country> Country {get; set;} 

}

Now I want to bind it to two combo boxes: The first combobox is fine, but on the second I want to display a list of countries.

 <custom:ComboBox  Title="Tab" 
               ItemsSource="{Binding Tabs, Mode=TwoWay}"
               ValuePath="Id"
               Value="{Binding Model.Id, Mode=TwoWay}"
               DisplayPath="Name"
               IsEnabled="{Binding IsEnabled, Mode=TwoWay}"/>


 <custom:ComboBox  Title="Country" 
                SelectedItem="{Binding Model.Country, Mode=TwoWay}"
                ItemsSource="{}" 
                DisplayPath="CountryName"
                IsEnabled="{Binding IsEnabled, Mode=TwoWay}"/>

How to set ItemsSource in second combobox when I know Id. Another way than creating a varible, like a selectedList, and then binding to it?

EDIT

enter image description here

I create a new dialog box and I submit the model with the id tab and dialog box. List of tabs.

+4
source share
1 answer

Give your first a ComboBoxname with x:Name="FirstComboBox"and change your ItemsSourcesecond ComboBox to ItemsSource="{Binding ElementName=FirstComboBox, Path=SelectedItem.Country}".

Just a hint: if collection binding in xaml uses ObservableCollection<T>instead List<T>.

+4

Source: https://habr.com/ru/post/1672345/


All Articles