WPF的MVVM模式给ComboBox绑定数据和读取

在网上找到类似的代码:

XAML文件

<ComboBox Margin="-16,3,0,5" Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="2" Name="cboxLocationKeyword"
 ItemsSource="{Binding LocationSource,Mode=OneWay}"
 SelectedValuePath="ID"
 DisplayMemberPath="Info"
 SelectedItem="{Binding SelectLocation}" />

对应的ViewModel文件

复制代码
public class LocationRoad
{
    public int ID { set; get; }
    public string Code { set; get; }
    public string Info { set; get; }
}
//
/// 当ComboBox选中项更改时发生
///
private LocationRoad _selectLocation;
public LocationRoad SelectLocation
{
    get
    {
        return this._selectLocation;
    }
    set
    {
        this._selectLocation = value;
        if (this.PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs("SelectLocation"));
    }
}
 
private ObservableCollection _locationRoad = null;
 
public ObservableCollection LocationSource
{
    get
    {
        if (this._locationRoad == null)
        {
            this._locationRoad = new ObservableCollection() {
                 new LocationRoad() { ID = 1, Code = "NGQ", Info = "南岗区" },
                 new LocationRoad() { ID = 2, Code = "DLQ", Info = "道里区" },
                 new LocationRoad() { ID = 3, Code = "DWQ", Info = "道外区" },
                 new LocationRoad() { ID = 4, Code = "PFQ", Info = "平房区" },
                 new LocationRoad() { ID = 5, Code = "XFQ", Info = "香坊区" },
                 };
 
        }
        return this._locationRoad;
    }
    set
    {
        this._locationRoad = value;
        if (this.PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs("LocationSource"));
    }
}
复制代码

如果要进行双向绑定或其他的绑定方式,只要更改上面binging块中的Mode方式就可以了。一般用不到吧?

 

 

出处:https://bbs.csdn.net/topics/390091439

============================================

我根据上面的代码,做了一点修改

<ComboBox Name="CbClientType" SelectedIndex="0" SelectedValuePath="Code" DisplayMemberPath="Descript"
SelectedItem="{Binding CbClientTypeSelectedItem}" MinWidth="70"></ComboBox>

对应的ViewModel代码

复制代码
        public CustomNodeItem _CbClientTypeSelectedItem;
        public CustomNodeItem CbClientTypeSelectedItem
        {
            get { return _CbClientTypeSelectedItem; }
            set
            {
                _CbClientTypeSelectedItem = value;
                //if (SelectCallBack != null)
                //{
                //    SelectCallBack(value);
                //}
                NotifyOfPropertyChange();
            }
        }

        private List<CustomNodeItem> _CbClientType;
        public List<CustomNodeItem> CbClientType
        {
            get { return _CbClientType; }
            set
            {
                _CbClientType = value;
                if (value != null && value.Count > 0 && CbClientTypeSelectedItem == null)
                {
                    CbClientTypeSelectedItem = value.First();
                }
                NotifyOfPropertyChange();
            }
        }

        //在按钮的事件中获取选择项
        public void BtnTest()
        {
            string msg = CbClientTypeSelectedItem.Descript;
            MessageBox.Show(msg);
        }
复制代码

 说明:这个应用在Caliburn.Micro中使用的

posted on   jack_Meng  阅读(7622)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2019-03-23 WinForm控件开发总结目录
2019-03-23 WinForm关于更新程序的设计思路
2019-03-23 Linux 下十大命令行下载工具
2019-03-23 C# EnumHelper Enum的值,Description,ToString()的相互转换
2019-03-23 DEV中的TreeList控件应用的一个小效果实现——个人总结
2019-03-23 DevExpress 控件汉化代码和使用方法
2019-03-23 Devexpress之LayoutControl的使用及其控件布局设计

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示

喜欢请打赏

扫描二维码打赏

支付宝打赏

主题色彩