C# WPF mvvm模式下combobox绑定(list<enum>、Dictionary<int,string>)

ComboBox是一个非常常用的界面控件,它的数据源ItemsSource既可以绑定一个List列表,也可以是一个字典,本篇文章就讲这些内容展开讲解。

01

前言

      ComboBox是一个非常常用的下拉菜单界面控件,它的数据源ItemsSource既可以绑定一个List列表,也可以是一个字典,本篇文章就讲这些内容展开讲解。

      首先,讲解几个常用的属性概念:

ItensSource:用于指定下拉列表绑定的List<string>数据对象;

SelectedIndex :下拉列表中选中行的索引;

DisplayMemberPath:下拉列表中要显示的List<T>数据对象的列,因为List<T>数据对象可能会有多列;

SelectedValuePath:下拉列表中,对应与显示的List<T>数据对象的列,返回的List<T>数据对象的列;

02


绑定ObservableCollection<T>

① 第一种情况T为一个普通学生类时:

类的定义:

public class Students
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }

数据绑定:

<dxlc:LayoutItem
                        Margin="10,0,0,0"
                        FontSize="13"
                        Label="StudentName" HorizontalContentAlignment="Right">
                            <ComboBox
                            Width="150"
                            HorizontalAlignment="Left"
                            SelectedIndex="2"
                            DisplayMemberPath="Name"
                            ItemsSource="{Binding StudentList, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
                        </dxlc:LayoutItem>

viewmodel中:

public ObservableCollection<Students> StudentList { get; set; } = new ObservableCollection<Students>();
StudentList.Add(new Students() { ID = 1, Name = "xiao zhu"});
            StudentList.Add(new Students() { ID = 2, Name = "xiao Li" });
            StudentList.Add(new Students() { ID = 3, Name = "xiao Wang" });
            StudentList.Add(new Students() { ID = 4, Name = "xiao Zhang" });

②第二种情况T为HumanSkinColors的枚举类型时:

枚举定义:

public enum HumanSkinColors
{
        Yellow,
        White,
        Black
    }

数据绑定:

<dxlc:LayoutItem
                        Margin="10,0,0,0"
                        FontSize="13"
                        Label="HumanSkinColor" HorizontalContentAlignment="Right">
                            <dxe:ComboBoxEdit
                            Width="150"
                            HorizontalAlignment="Left"
                            SelectedIndex="0"
                            ItemsSource="{Binding HumanSkinList, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
                        </dxlc:LayoutItem>

viewmodel代码:

public ObservableCollection<Students> StudentList { get; set; } = new ObservableCollection<Students>();
foreach (HumanSkinColors HumanSkinColor in Enum.GetValues(typeof(HumanSkinColors)))
            {
                HumanSkinList.Add(HumanSkinColor);
            }

03


绑定Dictionary<T,T>

字典的定义:

public Dictionary<int,string> StudentDic { get; set; } = new Dictionary<int, string>();

数据绑定:

<dxlc:LayoutItem
                        Margin="10,0,0,0"
                        FontSize="13"
                        Label="StudentName" HorizontalContentAlignment="Right">
                            <ComboBox
                            Width="150"
                            HorizontalAlignment="Left"
                            SelectedIndex="3"
                            DisplayMemberPath="Value"
                            SelectedValuePath="Key" 
                            ItemsSource="{Binding StudentDic}" />
                        </dxlc:LayoutItem>

viewmodel代码:

StudentDic.Add(1, "xiao zhu");
            StudentDic.Add(2, "xiao Li");
            StudentDic.Add(3, "xiao Wang");
            StudentDic.Add(4, "xiao Zhang");

04


结果展示

7639adfc3d1a435404c76206a603c1d4.png

技术群:添加小编微信并备注进群

小编微信:mm1552923   

公众号:dotNet编程大全 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值