WPF---ComboBox数据绑定
程序员文章站
2022-06-07 17:59:27
...
刚刚开始学习WPF,今天用到了ComboBox绑定数据,简单记录一下:
1、定义要绑定的数据类型
public class ConverterModel{
public List<ColorInfo> ColorList {get;set;}
public ConverterModel(){
ColorList = new List<ColorInfo>{
new ColorInfo(){ID = 0, Name = "Red"},
new ColorInfo(){ID = 1, Name = "Green"},
new ColorInfo(){ID = 2, Name = "Blue"}
};
}
}
public class ColorInfo{
public int ID{get;set;}
public string Name{get;set;}
}
2、xaml代码:
<ComboBox Name="comboBox" ItemsSource="{Binding ColorList}" SelectedValuePath="ID" DisplayMemberPath="Name">
3、xaml.cs代码;
comboBox.DataContext = new ConverterModel();
注意:ItemsSource = “{Binding ColorList}”
comboBox.DataContext = new ConverterModel();
上一篇: Java 8 Optional 类 解决空指针异常
下一篇: WPF中DataGrid绑定数据显示