我的目标是绑定SelectedItem。文本到文本框。但是,可以有多个选项,因此如果用户从组合框中选择一个选项,则可以选择另一个选项。在这种情况下,它会将第一个选项的第二个选项添加到文本框中。
private void txtTrnInter_SelectionChanged(object sender, RoutedEventArgs e)
{
foreach (var item in cmbInter.Items)
{
txtTrnInter.Text += item.ToString();
}
}
TextBox的输出应为:"ComboBoxItem1"+"ComboBboxItem2"+"comoBoxItem3"等。
试试这个:
private void cmbInter_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string selectedItem = cmbInter.SelectedItem.ToString();
txtTrnInter.Text += cmbInter.SelectedItem.ToString() + " ";
}
XAML:
<ComboBox x:Name="cmbInter" xmlns:s="clr-namespace:System;assembly=mscorlib"
SelectionChanged="cmbInter_SelectionChanged">
<s:String>a</s:String>
<s:String>b</s:String>
<s:String>c</s:String>
</ComboBox>
<TextBox x:Name="txtTrnInter" />
对于ComboBox
中的每个选择,它将把当前选择的项目添加到TextBox
中。