定义ComboBox,以及SelectionChanged事件
cs
<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"/>
实体类及数据绑定
cs
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事件的处理
cs
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;
}
//...
}