wpf ComboBox绑定数据及变更事件

定义ComboBox,以及SelectionChanged事件

 

实体类及数据绑定

public class ComboBoxEntity
{
    public string Key { get; set; }
    public string Value { get; set; }
}

cmb_radius.Items.Clear();
List lcb = new List();
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;
    }
    //...
}

 

 

你可能感兴趣的:(C#,WPF,c#,wpf)