定义ComboBox,以及SelectionChanged事件
<ComboBox x:Name="cmb_radius" Height="30" Width="65" FontSize="15"
DisplayMemberPath="Value" SelectedValuePath="Key" HorizontalAlignment="Center"
VerticalAlignment="Center" Margin="3" SelectionChanged="cmb_radius_SelectionChanged"/>
实体类及数据绑定
public class ComboBoxEntity
{public string Key { get; set; }public string Value { get; set; }
}cmb_radius.Items.Clear();
List<ComboBoxEntity> lcb = new List<ComboBoxEntity>();
for (int i = 0; i < M_Radius.Columns.Count; i++)
{ComboBoxEntity cbe = new ComboBoxEntity();cbe.Key = M_Radius.Columns[i].ColumnName;cbe.Value = M_Radius.Rows[0][i].ToString();// 添加ComboBoxEntity对象到列表中lcb.Add(cbe);
}// 设置cmb_radius的ItemsSource为lcb
cmb_radius.ItemsSource = lcb;
SelectionChanged事件的处理
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{ComboBox comboBox = (ComboBox)sender;// 获取选中的项object selectedValue = comboBox.SelectedItem;// 需要的业务逻辑处理if (selectedValue != null){string cbKey = (selectedValue as ComboBoxEntity).Key;string cbValue = (selectedValue as ComboBoxEntity).Value;}//...
}