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 ...
随机推荐
- java 关键字static
在Java中static表示“全局”或者“静态”的意思,用来修饰成员变量和成员方法,当然也可以修饰代码块. Java把内存分为栈内存和堆内存, 栈内存用来存放一些基本类型的变量.数组和对象的引用, 堆 ...
- ChemDraw Pro和ChemBio3D Ultra有什么区别
作为ChemOffice 14的两个重要组件,ChemDraw Pro和ChemBio3D Ultra都有着自己的特点,ChemDraw Pro以绘制平面结构而ChemBio3D Ultra以绘制立体 ...
- C语言程序设计-猴子选大王[链表应用]
2032 猴子选大王 Description 有N只猴子,从1~N进行编号.它们按照编号的顺时针方向排成一个圆圈,然后从第一只猴子开始报数.第一只猴子报的第一个数字为1,以后每只猴子报的数字都是它们前 ...
- c/c++中内存对齐完全理解
一,什么是内存对齐?内存对齐用来做什么? 所谓内存对齐,是为了让内存存取更有效率而采用的一种编译阶段优化内存存取的手段. 比如对于int x;(这里假设sizeof(int)==4),因为cpu对内存 ...
- pandas基础: Series和DataFrame的简单介绍
一.pandas 是什么 pandas 是基于 NumPy 的一个 Python 数据分析包,主要目的是为了数据分析.它提供了大量高级的数据结构和对数据处理的方法. pandas 有两个主要的数据结构 ...
- java的junit測试
在实际的开发中不仅须要断点调试.语句输出的方法进行程序的调试,也须要单元測试. 在java中的junit的測试方法通常是在要測试的方法上面加入@Test.@ Before.@After,@Before ...
- win7下安装memcached
memcached server端服务在win7下的安装.启动图解 1.首先下载解压memcached-1.2.6-win32-bin.zip到某一盘下,如下图 2.通过管理员方式运行cmd.exe. ...
- JS循环语句的理解
循环语句就是让程序重复性去做某些工作 最常见的就是for循环 那它的写法都有哪些呢? 1.必须要有初始值 2.要有条件判断 3.状态的改变 4.循环体 一定要控制循环多少次结束,否则就变成了死循环,消 ...
- Nginx的安装与基本应用
web服务器软件IIS (windows底下的web服务器软件) Nginx (Linux底下新一代高性能的web服务器) Tengine www.taobao.com 这是淘宝 Apache (Li ...
- Software Defined Networking For Dummies, Cisco Special Edition
从接触SDN开始,不论是硬件还是软件,一直都是从具体的点开始,慢慢的勾勒出自己认为的SDN的样子,相信读完这本思科出的关于SDN的书会对其有新的认识 这本书的名字就是Software Defined ...