WPF学习笔记:ComboBox的数据绑定
来源:互联网 发布:js 不等于空字符串 编辑:程序博客网 时间:2024/05/16 10:56
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
0 0
- WPF学习笔记:ComboBox的数据绑定
- WPF 数据绑定 combobox
- WPF MVVM 绑定ComboBox数据
- WPF学习笔记之-WPF数据绑定
- wpf combobox数据绑定并添加一个额外的选项
- WPF的ComboBox数据绑定,使用Dictionary作为数据源
- WPF数据绑定的学习
- wpf学习笔记-数据绑定功能总结
- WPF学习笔记之数据绑定
- WPF学习笔记:数据绑定与DataContext
- wpf ComboBox 控件利用DataSet数据绑定
- 【WPF】给下拉列表ComboBox绑定数据
- ComboBox数据的绑定
- [WPF学习]数据绑定
- WPF学习----数据绑定
- WPF数据绑定学习
- WPF的数据绑定笔记摘录
- WPF and Silverlight 学习笔记(二十二):使用代码实现绑定、绑定数据的验证
- SGU 102 prime水题
- Linux socket编程介绍
- Vim的使用
- Github的使用
- 简述xml
- WPF学习笔记:ComboBox的数据绑定
- 交叉编译directfb - Cross compile directfb
- freelancer 语源
- jackson处理json对象
- Spring MVC 学习笔记 json格式的输入和输出
- shell整理
- Oracle学习(十三):闪回
- 抽象工厂模式 (C语言实现)
- struts2零配置(2)