2016-11-24 126 views
0

如果我有一个组合框中的项目是CFG_REG,INT_REG,ST_REG,CMD_REG(它们在枚举中定义),如果我选择项目CFG_REG,那么我应该在另一个组合框中同样显示GCR,PCR,LCR,CR,GSR,PSR,如果我选择INT_REG,我应该显示IE,就像那样,..我该怎么做?如何根据wpf中的另一个combox项目来绑定combobox中的itemsource?

 <ComboBox Grid.Column="2" 
         Grid.Row="1" 
         SelectedIndex="{Binding CMDIndex, Mode=TwoWay}" 
         x:Name="Combobox1" 
         Margin="0,0,1,0" 
         VerticalAlignment="Top"> 
        </ComboBox> 

    <ComboBox Grid.Column="3" IsTextSearchEnabled="True" 
         Grid.Row="1" 
         x:Name="combobox2" 
         ItemsSource="{Binding }" 
         SelectedItem="{Binding RegisterIndex,Mode=TwoWay}" 
         VerticalAlignment="Top" IsSynchronizedWithCurrentItem="True" DisplayMemberPath="Name" 
         Margin="0,0,1,0"> 

        </ComboBox> 

回答

1

你应该的项目在您的视图模型/代码的集合(即ICollection的或观察到的集合)绑定到第一个组合框的的ItemsSource。您可以将第一个组合框的'SelectedItem'绑定到/ view模型后面的代码中的属性,然后在该属性的setter中,您应该过滤掉另一个将绑定到其他组合框的Collection。我希望你明白这个主意。

例如:

<ComboBox ItemsSource ={Binding Collection1} SelectedItem ={Binding SelectedItem} .../> 

在代码:

public ICollection Collection1 {get;set;} 
public ICollection Collection2 {get;set;} 

public string SelectedItem 
{ 
get {..} 
set{ 
SelectedItem = value; 
ChangeSecondCollection(value); 
} 

public void ChangeSecondCollection(string value) 
{ 
Collection2 = //Filter your second collection here. 
} 
+0

对不起,我没有得到,你可以用elabarate上面的例子@罗希特 - GARG我提到 –

相关问题