UI

<UserControl x:Class="UnitViews.UserMeUV"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<ComboBox x:Name="cbStatus" ItemsSource="{Binding StatusList}"
SelectedValuePath="Key" DisplayMemberPath="Value" SelectedItem="{Binding SelectedStatus}"
Height="30">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding SelectionChangedCmd}"
CommandParameter="{Binding ElementName=cbStatus}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
</Grid>
</UserControl>

VM

        static Dictionary<User.EStatus, string> olist = new Dictionary<User.EStatus, string>
{
{1,"有钱" }
,{2,"有闲" }
,{3,"有料" }
,{4,"有鬼" }
};
public Dictionary<User.EStatus, string> StatusList
{
get
{
return olist;
}
}
KeyValuePair<User.EStatus, string> _kvp;
public KeyValuePair<User.EStatus, string> SelectedStatus
{
get
{
return this._kvp;
}
set
{
this._kvp = value;
this.RaisePropertyChanged("SelectedStatus");
}
}
DelegateCommand<ComboBox> _SelectionChangedCmd = null;
public DelegateCommand<ComboBox> SelectionChangedCmd
{
get
{
if (this._SelectionChangedCmd == null)
{
this._SelectionChangedCmd = new DelegateCommand<ComboBox>(SelectionChanged);
} return this._SelectionChangedCmd;
}
}
void SelectionChanged(ComboBox cb)
{
SelectedStatus = (KeyValuePair<User.EStatus, string>)cb.SelectedItem;
Status = SelectedStatus.Key;
}
public User.EStatus Status
{
set
{
var query = olist.Where(n => n.Key == value);
if (query != null && query.Count() > 0)
{
this.SelectedStatus = query.First();
}
}
}

注意里面用了Dictionary<>作为数据源的类型,因此有ComboBox控件里有

SelectedValuePath="Key" DisplayMemberPath="Value"

参考文章:

http://www.luacloud.com/2011/wpf-combobox-binding-data.html

WPF学习笔记:ComboBox的数据绑定的更多相关文章

  1. WPF学习笔记-用Expression Design制作矢量图然后导出为XAML

    WPF学习笔记-用Expression Design制作矢量图然后导出为XAML 第一次用Windows live writer写东西,感觉不错,哈哈~~ 1.在白纸上完全凭感觉,想象来画图难度很大, ...

  2. WPF 学习笔记-在WPF下创建托盘图标

    原文:WPF 学习笔记-在WPF下创建托盘图标 首先需要在项目中引用System.Windows.Forms,System.Drawing; using System; using System.Co ...

  3. WPF 学习笔记-设置属性使窗口不可改变大小

    原文:WPF 学习笔记-设置属性使窗口不可改变大小 调整Windows下的ResizeMode属性: ResizeMode = NoResize Resize属性是控制Windows是否可以改变大小, ...

  4. WPF学习笔记(8):DataGrid单元格数字为空时避免验证问题的解决

    原文:WPF学习笔记(8):DataGrid单元格数字为空时避免验证问题的解决 如下图,在凭证编辑窗体中,有的单元格不需要数字,但如果录入数字后再删除,会触发数字验证,单元格显示红色框线,导致不能执行 ...

  5. WPF学习(8)数据绑定

    说到数据绑定,其实这并不是一个新的玩意儿.了解asp.net的朋友都知道,在asp.net中已经用到了这个概念,例如Repeater等的数据绑定.那么,在WPF中的数据绑定相比较传统的asp.net中 ...

  6. WPF学习(8)数据绑定 https://www.cnblogs.com/jellochen/p/3541197.html

    说到数据绑定,其实这并不是一个新的玩意儿.了解asp.net的朋友都知道,在asp.net中已经用到了这个概念,例如Repeater等的数据绑定.那么,在WPF中的数据绑定相比较传统的asp.net中 ...

  7. 【WPF学习笔记】之如何把数据库里的值读取出来然后显示在页面上:动画系列之(六)(评论处有学习资料及源码)

    (应博友们的需要,在文章评论处有源码链接地址,以及WPF学习资料.工具等,希望对大家有所帮助) ...... 承接系列五 上一节讲了,已经把数据保存到数据库并且删除数据,本讲是把已经存在的数据从数据库 ...

  8. WPF 学习笔记(一)

    一.概述 WPF(Windows Presentation Foundation)是微软推出的基于Windows 的用户界面框架,随着.NET Framework 3.0发布第一个版本.它提供了统一的 ...

  9. WPF学习笔记:(一)数据绑定与DataContext

    前一段半心半意地学习了一下WPF,是从控件入手的,发现巨容易,甚至有些无趣.昨天面试,被问到了很多WPF的特性的东西,直接就傻了.于是乎,还是要去深刻的学习一下WPF.刚刚试了一下数据绑定,几次都没有 ...

随机推荐

  1. centos如何离线安装部署node&pm2?

    最近我们项目要上即时通讯,因为项目对安全要求比较高,所以选择了即时通讯云服务器yun2win,他们提供了数据服务器让我们自己安装部署.那么问题来了,我们服务器是放在内网,完全无法访问外网,而yun2w ...

  2. 安装mask-rcnn问题汇总

    1. I download file from https://github.com/waleedka/coco. Then I placed the file in Mask_RCNN-master ...

  3. BZOJ 3884: 上帝与集合的正确用法 扩展欧拉定理 + 快速幂

    Code: #include<bits/stdc++.h> #define maxn 10000004 #define ll long long using namespace std; ...

  4. 查询条件中,不进sql语句 也不进后台bug

    前端代码:本来代码中少写了value="1",后来加上value值之后,可以正常进方法 <div class="row"> <label cl ...

  5. valgrind检查代码内存泄漏,5种内存泄漏情况

    摘要: valgrind是linux下用于调试程序和查找内存泄露的常用工具.valgrind会报告5种内存泄露,"definitely lost", "indirectl ...

  6. 这份Koa的简易Router手敲指南请收下

    上一期链接--也就是本文的基础,参考KOA,5步手写一款粗糙的web框架 本文参考仓库:点我 Router其实就是路径匹配,通过匹配路径,返回给用户相应的网站内容. 以下方例子为例,主要通过提取req ...

  7. [Algorithm] 10. Reverse Integer

    Description Given a 32-bit signed integer, reverse digits of an integer. Example Example 1: Input: 1 ...

  8. python正则表达式提取字符串

    用python正则表达式提取字符串 在日常工作中经常遇见在文本中提取特定位置字符串的需求.python的正则性能好,很适合做这类字符串的提取,这里讲一下提取的技巧,正则表达式的基础知识就不说了,有兴趣 ...

  9. idea 如何将本地代码上传到github

    1. 首先切换到项目根目录下 执行 git init 2. 点击项目右键->Git->Repository->Remotes->编辑URL 到github代码地址.

  10. 【06】AngularJS 控制器

    AngularJS 控制器 AngularJS 控制器 控制 AngularJS 应用程序的数据. AngularJS 控制器是常规的 JavaScript 对象. AngularJS 控制器 Ang ...