WPF MVVM模式下ComboBox级联效果 选择第一项
MVVM模式下做的省市区的级联效果。通过改变ComboBox执行命令改变市,区。
解决主要问题就是默认选中第一项
1.首先要定义一个属性,继承自INotifyPropertyChanged接口。我这里用的Prism框架中集成的NotificationObject
/// <summary>
/// 省
/// </summary>
private ObservableCollection<MyArea> provinceBindingList;
public ObservableCollection<MyArea> ProvinceBindingList
{
get { return provinceBindingList; }
set { provinceBindingList = value; this.RaisePropertyChanged("ProvinceBindingList"); }
}
/// <summary>
/// 市
/// </summary>
private ObservableCollection<MyArea> cityBindingList;
public ObservableCollection<MyArea> CityBindingList
{
get { return cityBindingList; }
set { cityBindingList = value; this.RaisePropertyChanged("CityBindingList"); }
}
/// <summary>
/// 区
/// </summary>
private ObservableCollection<MyArea> areaBindingList;
public ObservableCollection<MyArea> AreaBindingList
{
get { return areaBindingList; }
set { areaBindingList = value; this.RaisePropertyChanged("AreaBindingList"); }
} /// <summary>
/// 默认选择请选择项
/// </summary>
public readonly MyArea defaultSelectItem;
/// <summary>
/// 添加城市选择项
/// </summary>
private MyArea selectCity;
public MyArea SelectCity
{
get { return selectCity; }
set { selectCity = value; this.RaisePropertyChanged("SelectCity"); }
}
属性定义
2.XAML部分
<ComboBox SelectedIndex="0" ItemsSource="{Binding ProvinceBindingList,Mode=TwoWay}"
SelectedItem="{Binding defaultSelectItem,Mode=TwoWay}" DisplayMemberPath="Name"
Margin="5,105,5,5" Width="100" Name="cboProvince" Style="{StaticResource ComboBoxStyle}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding Path=DataContext.GetCity,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ComboBox}}"
CommandParameter="{Binding Path=SelectedValue,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ComboBox}}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
<ComboBox SelectedIndex="0" ItemsSource="{Binding CityBindingList,Mode=TwoWay}" DisplayMemberPath="Name"
SelectedItem="{Binding SelectCity,Mode=TwoWay}" Margin="5" Width="100" Grid.Row="1"
Name="cboCity" Style="{StaticResource ComboBoxStyle}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding Path=DataContext.GetArea,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ComboBox}}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
<ComboBox SelectedIndex="0" ItemsSource="{Binding AreaBindingList,Mode=TwoWay}" DisplayMemberPath="Name"
SelectedItem="{Binding SelectCity,Mode=TwoWay}" Margin="5" Width="100" Grid.Row="2"
Name="cboArea" Style="{StaticResource ComboBoxStyle}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding Path=DataContext.GetAddCityInfoExecute,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ComboBox}}"
CommandParameter="{Binding Path=SelectedValue,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ComboBox}}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
3.命令部分
//初始化命令
this.GetProvince = new DelegateCommand(GetProvinceExecute);
this.GetCity = new DelegateCommand<MyArea>(GetCityExecute);
this.GetArea = new DelegateCommand(GetAreaExecute);
/// <summary>
/// 获取省
/// </summary>
private void GetProvinceExecute()
{
var province = AreaManager.GetProvince();
province.Insert(, defaultSelectItem);
ProvinceBindingList = new ObservableCollection<MyArea>(province);
} /// <summary>
/// 获取市
/// </summary>
/// <param name="obj"></param>
private void GetAreaExecute()
{
if (SelectCity != null && SelectCity.ID != "")
{
var area = AreaManager.GetAreaByCID(SelectCity);
area.Insert(, defaultSelectItem);
AreaBindingList = new ObservableCollection<MyArea>(area);
}
else if (SelectCity == null || SelectCity != null && SelectCity.ID == "")
AreaBindingList = new ObservableCollection<MyArea>(new List<MyArea>() { defaultSelectItem });
} /// <summary>
/// 获取区
/// </summary>
/// <param name="obj"></param>
private void GetCityExecute(MyArea province)
{
if (province != null && province.ID != "")
{
var city = AreaManager.GetCityByPID(province);
city.Insert(, defaultSelectItem);
CityBindingList = new ObservableCollection<MyArea>(city);
SelectCity = defaultSelectItem;
}
else if (province == null || province != null && province.ID == "")
CityBindingList = new ObservableCollection<MyArea>(new List<MyArea>() { defaultSelectItem });
}
命令对应方法
WPF MVVM模式下ComboBox级联效果 选择第一项的更多相关文章
- wpf mvvm模式下CommandParameter传递多参
原文:wpf mvvm模式下CommandParameter传递多参 CommandParameter一般只允许设置一次,所以如果要传递多参数,就要稍微处理一下.我暂时还没找到更好的方案,下面介绍的这 ...
- WPF MVVM模式下的无阻塞刷新探讨
很多时候我们需要做一个工作,在一个方法体里面,读取大数据绑定到UI界面,由于长时间的读取,读取独占了线程域,导致界面一直处于假死状态.例如,当应用程序开始读取Web资源时,读取的时效是由网络链路的速度 ...
- WPF MVVM模式下路由事件
一,路由事件下三种路由策略: 1 冒泡:由事件源向上传递一直到根元素.2直接:只有事件源才有机会响应事件.3隧道:从元素树的根部调用事件处理程序并依次向下深入直到事件源.一般情况下,WPF提供的输入事 ...
- WPF MVVM模式下实现ListView下拉显示更多内容
在手机App中,如果有一个展示信息的列表,通常会展示很少一部分,当用户滑动到列表底部时,再加载更多内容.这样有两个好处,提高程序性能,减少网络流量.这篇博客中,将介绍如何在WPF ListView中实 ...
- wpf mvvm模式下 在ViewModel关闭view
本文只是博主用来记录笔记,误喷 使用到到了MVVM中消息通知功能 第一步:在需要关闭窗体中注册消息 public UserView() { this.DataContext = new UserVie ...
- wpf mvvm模式下的image绑定
view文件 <Image Grid.Column="2" Width="48" Height="64" Stretch=" ...
- MVVM模式下WPF动态绑定展示图片
MVVM模式下WPF动态展示图片,界面选择图标,复制到项目中固定目录下面,保存到数据库的是相对路径,再次读取的时候是根据数据库的相对路径去获取项目中绝对路径的图片展示. 首先在ViewModel中 / ...
- Silverlight中在MVVM模式下对DatagridRow选择控件封装
在项目中,凡是涉及到表格的地方用的最多的控件,自然少不了DataGrid的身影,它明了的展示各种数据让人十分喜欢.现在要实现一个功能,使DataGrid具有全选和项选中的功能,如果在传统后台代码中完成 ...
- WPF中在MVVM模式下,后台绑定ListCollectionView事件触发问题
问题:WPF中MVVM模式下 ListView绑定ListCollectionView时,CurrentChanged无法触发 解决方案: 初期方案:利用ListView的SelectionChang ...
随机推荐
- 向量类Vector
Java.util.Vector提供了向量(Vector)类以实现类似动态数组的功能.在Java语言中.正如在一开始就提到过,是没有指针概念的,但如果能正确灵活地使用指针又确实可以大大提高程序的质量, ...
- 【BZOJ】3410: [Usaco2009 Dec]Selfish Grazing 自私的食草者(贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=3410 太神了.... 按末端点排序然后贪心取即可. QAQ #include <cstdio& ...
- 添加RichEdit控件后导致MFC对话框程序无法运行的解决方法
新建一个基于对话框的MFC程序,对话框上添加了RichEdit控件,编译成功后无法运行起来,Debug版本与Release版本均不行! Windbg分析结果: WARNING: Stack unwin ...
- js 跳转的几种方法收藏
history.go(-n) 返回上一页(n 为返回前几页) window.location.reload(); 刷新当前页面 history.go(-1);window.locatoin.reloa ...
- Linux严格区分大小写
虚拟机上安装了MySQL,使用rpm -qa | grep mysql查询时却未找到安装的mysql,后面才发现Linux严格区分大小写,正确的查询命令应该为rpm -qa | grep MySQL, ...
- Poj1426
Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25721 Accepted: 106 ...
- angualar入门学习-- 自定义指令 指令编译执行过程
3个阶段: 一.加载阶段 加载angular.js的源码,找到ng-app确定应用边界范围. 二.编译阶段 compile 查找所有指令,保存在一个列表中 对所有指令按优先级(property属性值) ...
- 原生JS返回顶部,带返回效果
有些网站当滑到一定高度时右下角会有一个按钮,你只要一点就可以直接返回顶部了.那这个功能是怎么做到的呢.其实不算太难: 首先我们先在网页中创建一个按钮,上面写上返回顶部,把它的样式改成固定定位,之后想要 ...
- CodeForces 666A Reberland Linguistics(DP)
A. Reberland Linguistics time limit per test 1 second memory limit per test 256 megabytes input stan ...
- 测试一个服务器的性能,客户要求向数据库(Sqlserver2012)内 1000/s(每插入一千条数据) 的处理能力
通过jmeter很简单就可以完成,可以参考我以前的一篇文章<jmeter创建数据库(Sqlserver2012)测试>. 前提条件:一个数据库:test 数据库下面有一张表:user ...