MVVM模式下ComboBox绑定

本文介绍了一种使用C#进行数据绑定的方法,具体展示了如何定义CommonBox类,包括Namepath和Value属性,并将其用于ComboBox控件的数据源。通过实例演示了如何向List集合中添加CommonBox对象并设置为ComboBox的ItemsSource,以及如何将其中一个对象设为SelectedItem。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

定义 :

 

class CommonBox
    {
        public string Namepath { get; set; }
        public int Value { get; set; }
    }
//检测线路
        public List<CommonBox> checkLines { get; set; }
        public CommonBox checkLine { get; set; }

 this.checkLines = new List<CommonBox>();
 this.checkLines.Add(new CommonBox() { Namepath = "线路1", Value = 1 });
            this.checkLines.Add(new CommonBox() { Namepath = "线路2", Value = 1 });
            this.checkLine = this.checkLines[0];


xaml

 ItemsSource 绑定数据源,

 SelectedItem 当前项,

checkLine.Namepath != " " 判断当前项目是不是为空

 

<Label Content="检测线路:" HorizontalAlignment="Left" Margin="0,10,0,0" VerticalAlignment="Top"/>
                            <ComboBox HorizontalAlignment="Left" Margin="61,10,0,0" VerticalAlignment="Top" Width="120" 
                                      ItemsSource="{Binding checkLines}"
                                      DisplayMemberPath="Namepath"
                                      SelectedItem="{Binding checkLine}"/>

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值