2017-03-15 31 views
4

我有选项卡的列表,标签是:WPF绑定到组合框

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

} 

现在,我想将它绑定两个组合框: 第一组合框是好的,但在第二个我想显示的国家列表。

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

如何在第二个组合框中设置ItemsSource,当我知道Id。 是另一种方式比创建变量,像selectedList,然后绑定到它?

编辑

enter image description here

我创造新的对话框,我送模型标签ID和对话框方面有选项卡列表。

回答

4

x:Name="FirstComboBox"给你的第一ComboBox名称和改变你的第二ComboBoxto ItemsSource="{Binding ElementName=FirstComboBox, Path=SelectedItem.Country}"ItemsSource

只是一个提示:绑定xaml集合时使用ObservableCollection<T>而不是List<T>

+0

第二个组合框仅在我从第一个组合框手动选择值时重新加载。当我自动绑定,第二个组合框是空的,那么我必须首先从组合框中选择值,然后第二个组合框有值。我应该使用UpdateSourceTrigger = PropertyChanged或其他什么 – dMilan

+0

要更新第二个组合框,我必须先点击进入。如何自动更新第二个组合框? – dMilan

+0

你是什么意思自动? –