数据绑定-在ComboBox中(相对于其他组合框) [英] Data Binding- in ComboBox(relative to other combo box)
问题描述
大家好,
我有两个组合框,需要相对于前一个组合框设置后一个组合框的值.
例如,
组合框(I)-值A,B和C
ComboBox(II)-值0、1、2、3、4、5、6、7、8、9
用户选择(I)和类型A-(II)-值0,1,2
用户选择(I)和类型B-(II)-值1 ... 9
在用户选择(I)和类型C-(III)-仅值9时,需要选择.
如何实现上述更改.任何帮助都是可观的.谢谢,
问候,
Samanth_90
Hi All,
I have two combo box, and need to set the values of latter combobox relative to the former combo box.
for eg.,
Combobox(I) - values A, B & C
ComboBox(II) - values 0, 1, 2 ,3,4,5,6,7,8,9
On user Selection of (I) and Type A- (II) - values 0,1,2
On user Selection of (I) and Type B- (II) - values 1...9
On User selection of (I) and Type C- (III) - values 9 Only, needs selection.
How to effect the said changes. Any help is appreciable. Thanks.,
With Regards,
Samanth_90
推荐答案
您需要具有SelectionChanged
事件处理程序:
XAML代码:
You need to have aSelectionChanged
event handler :
XAML code :
<combobox height="23" horizontalalignment="Left" margin="12,41,0,0" name="comboBox1" verticalalignment="Top" width="120" selectionchanged="comboBox1_SelectionChanged">
<comboboxitem content="A" />
<comboboxitem content="B" />
<comboboxitem content="C" />
</combobox>
<combobox height="23" horizontalalignment="Left" margin="12,84,0,0" name="comboBox2" verticalalignment="Top" width="120" />
背后的代码:
在(窗口,页面或用户控件的)Loaded
事件处理程序中:
Code behind :
in Loaded
event handler (of your Window or Page or User Control) :
comboBox1.SelectedIndex = 0;
和SelectionChanged
事件处理程序:
and the SelectionChanged
event handler :
private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (comboBox2 == null)
return;
switch (comboBox1.SelectedIndex)
{
case 0:
comboBox2.ItemsSource = new List<int> { 0, 1, 2 };
break;
case 1:
comboBox2.ItemsSource = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
break;
case 2:
comboBox2.ItemsSource = new List<int> { 9 };
break;
}
comboBox2.SelectedIndex = 0;
}
希望对您有所帮助.
Hope it helps.
这篇关于数据绑定-在ComboBox中(相对于其他组合框)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!