当前位置:   article > 正文

wpf ComboBox 绑定数据_wpf combox数据绑定

wpf combox数据绑定

ComboBox 绑定 DataTable 数据

  1. DeviceTypeViewModel deviceTypeModel = new DeviceTypeViewModel();
  2. this.parentIdTxt.ItemsSource = deviceTypeModel.GetDeviceType().DefaultView;
  3. this.parentIdTxt.DisplayMemberPath = "name";
  4. this.parentIdTxt.SelectedValuePath = "id";

但我们经常会在诸如分类时添加顶级分类选项,所以我是这样做的,先把数据加入Dictionary,在使用Dictionary作为数据源

  1. public Dictionary<int, string> ComboboxItem()
  2. {
  3. DataTable deviceTypes = GetDeviceType();
  4. Dictionary<int, string> items = new Dictionary<int, string>();
  5. items.Add(0, "顶级分类");
  6. foreach (DataRow row in deviceTypes.Rows)
  7. {
  8. items.Add(Convert.ToInt32(row["id"]), row["name"].ToString());
  9. }
  10. return items;
  11. }
  1. DeviceTypeViewModel deviceTypeModel = new DeviceTypeViewModel();
  2. Dictionary<int, string> items = deviceTypeModel.ComboboxItem();
  3. this.parentIdTxt.ItemsSource = items;
  4. this.parentIdTxt.DisplayMemberPath = "Value";
  5. this.parentIdTxt.SelectedValuePath = "Key";

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/928612
推荐阅读
相关标签
  

闽ICP备14008679号