参考:https://www.cnblogs.com/xpvincent/p/3848790.html
参考:https://blog.csdn.net/zhudaokuan/article/details/109059333
前端:
<ComboBox Name="cmbBox" ItemsSource="{Binding list}" DisplayMemberPath="Code" SelectedValue="{Binding SelectedCode}" />
后台:
private CodeVo selectCode; public CodeVo SelectCode { get { return selectCode; } set { SetProperty(ref selectCode, value); } } public List<CodeVo> list { get; set; } public MainViewModel() { list = new List<CodeVo> { new CodeVo{ ID= "A1",Code = "广州" }, new CodeVo{ ID= "B1",Code = "重庆" }, new CodeVo{ ID= "C1",Code = "山东" }, new CodeVo{ ID= "D1",Code = "合肥" }, new CodeVo{ ID= "E1",Code = "石家庄" } }; selectCode = list[1]; }
这样就可以了,在界面上会默认显示 重庆,在后台取值时 SelectCode.Code 可以拿到选中值。
标签:CodeVo,Code,selectCode,MVVM,C#,ComboBox,public,new,ID From: https://www.cnblogs.com/z5337/p/17896914.html