WPF ComBox绑定数据(转)
WPF ComBox绑定数据
1、xmal文件中添加 DisplayMemberPath、SelectedValuePath属性
<ComboBox Name="MeaType" RenderTransformOrigin="1.72,2.273" Canvas.Left="48" Canvas.Top="226" Width="110" DisplayMemberPath="Name" SelectedValuePath="ID" SelectionChanged="MeaType_SelectionChanged" Loaded="MeaType_Loaded"> </ComboBox>
2、控件绑定的内容类
public class MeaType
{
public int ID{get;set;}
public string Name{get; set;}
}
3.代码中添加需要绑定的数据
List<MeaType> list = new List<MeaType>();
list.Add(new MeaType { ID = 1,Name = "S11" });
list.Add(new MeaType { ID = 2, Name = "S21" });
list.Add(new MeaType { ID = 3, Name = "S12" });
list.Add(new MeaType { ID = 4, Name = "S22" });
MeaType.ItemsSource = list;
4、代码中更改绑定