欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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();

相关标签: WPF 数据绑定