[WPF]ComboBox的使用方法

来源:赵克立博客 分类: C# 标签:C#WPF发布时间:2017-09-18 15:32:50最后更新:2019-07-11 10:21:32浏览:5315
版权声明:
本文为博主原创文章,转载请声明原文链接...谢谢。o_0。
更新时间:
2019-07-11 10:21:32
温馨提示:
学无止境,技术类文章有它的时效性,请留意文章更新时间,如发现内容有误请留言指出,防止别人"踩坑",我会及时更新文章

先定义一个类存放ComboBox第一项的数据信息一个id一个title两个属性

    public class Info:INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private int _id;
        public int id
        {
            get { return _id; }
            set
            {
                _id = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("id"));
                }
            }
        }
        private string _title;
        public string title
        {
            get { return _title; }
            set
            {
                _title = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("title"));
                }
            }
        }
    }

初始化ComboBox的值

//初始化组合框绑定信息
BindingList<Info> infoList = new BindingList<Info>();
cbox1.ItemsSource = infoList;
//设置哪个属性为可视文本
cbox1.DisplayMemberPath = "title";
//设置哪个属性为当前选中的值
cbox1.SelectedValuePath = "id";
infoList.Add(new Info() { id = 1, title = "张三" });
infoList.Add(new Info() { id = 2, title = "李四" });
infoList.Add(new Info() { id = 3, title = "王五" });
//设置当前选中的一项
cbox1.SelectedValue = 1;

image.png


微信号:kelicom QQ群:215861553 紧急求助须知
Win32/PHP/JS/Android/Python