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级联效果 选择第一项的更多相关文章

  1. wpf mvvm模式下CommandParameter传递多参

    原文:wpf mvvm模式下CommandParameter传递多参 CommandParameter一般只允许设置一次,所以如果要传递多参数,就要稍微处理一下.我暂时还没找到更好的方案,下面介绍的这 ...

  2. WPF MVVM模式下的无阻塞刷新探讨

    很多时候我们需要做一个工作,在一个方法体里面,读取大数据绑定到UI界面,由于长时间的读取,读取独占了线程域,导致界面一直处于假死状态.例如,当应用程序开始读取Web资源时,读取的时效是由网络链路的速度 ...

  3. WPF MVVM模式下路由事件

    一,路由事件下三种路由策略: 1 冒泡:由事件源向上传递一直到根元素.2直接:只有事件源才有机会响应事件.3隧道:从元素树的根部调用事件处理程序并依次向下深入直到事件源.一般情况下,WPF提供的输入事 ...

  4. WPF MVVM模式下实现ListView下拉显示更多内容

    在手机App中,如果有一个展示信息的列表,通常会展示很少一部分,当用户滑动到列表底部时,再加载更多内容.这样有两个好处,提高程序性能,减少网络流量.这篇博客中,将介绍如何在WPF ListView中实 ...

  5. wpf mvvm模式下 在ViewModel关闭view

    本文只是博主用来记录笔记,误喷 使用到到了MVVM中消息通知功能 第一步:在需要关闭窗体中注册消息 public UserView() { this.DataContext = new UserVie ...

  6. wpf mvvm模式下的image绑定

    view文件 <Image Grid.Column="2" Width="48" Height="64" Stretch=" ...

  7. MVVM模式下WPF动态绑定展示图片

    MVVM模式下WPF动态展示图片,界面选择图标,复制到项目中固定目录下面,保存到数据库的是相对路径,再次读取的时候是根据数据库的相对路径去获取项目中绝对路径的图片展示. 首先在ViewModel中 / ...

  8. Silverlight中在MVVM模式下对DatagridRow选择控件封装

    在项目中,凡是涉及到表格的地方用的最多的控件,自然少不了DataGrid的身影,它明了的展示各种数据让人十分喜欢.现在要实现一个功能,使DataGrid具有全选和项选中的功能,如果在传统后台代码中完成 ...

  9. WPF中在MVVM模式下,后台绑定ListCollectionView事件触发问题

    问题:WPF中MVVM模式下 ListView绑定ListCollectionView时,CurrentChanged无法触发 解决方案: 初期方案:利用ListView的SelectionChang ...

随机推荐

  1. 位集合类BitSet

    位集合类中封装了有关一组二进制数据的操作. 我们先来看一下例8.6 BitSetApp.java. 例8.6 BitSetApp.java //import java.lang.*; import j ...

  2. Bootstrap的下拉菜单float问题

    在学习bootstrap中的下拉菜单时,遇到下面情况: <div class="dropdown"> <button class="btn btn-de ...

  3. Oracle11g Active Data Guard搭建、管理

    说明:參考网络众多人的笔记及思路,加上自己亲身实践之后的整理笔记.仅供參考. Data Guard与RAC不同的是.在普通情况下.Standby仅仅有一个节点处于活动状态,全部的应用都连接到主serv ...

  4. Office Developer Tools for Visual Studio 2012现在可用了

    [原文发表地址]   Now Available: Office Developer Tools for Visual Studio 2012 正如我以前写过的,我们正在为构建下一代Office和 S ...

  5. [Algorithms] Heap and Heapsort

    Recently I reviewed the classic heapsort algorithm and implement it according to contents in Introdu ...

  6. SpringBoot专题1----springboot与mybatis的完美融合

    springboot大家都知道了,搭建一个spring框架只需要秒秒钟.下面给大家介绍一下springboot与mybatis的完美融合: 首先:创建一个名为springboot-mybatis的ma ...

  7. 【Python之路】第二十三篇--Django【进阶篇】

    文件配置 1.模版Templates文件配置: TEMPLATE_DIRS = ( os.path.join(BASE_DIR,'templates'), ) 2.静态文件static配置: STAT ...

  8. to_base64 --- from_base64

    UPDATE traceroleid_copy SET Pwd=to_base64(Pwd) SELECT from_base64(Pwd) FROM traceroleid_copy

  9. Babel编译

    Babel的目的就是让你可以使用最新的标准来开发,然后把兼容的问题交给它来完成.比如我如何在使用ES6的语法写完之后将其转换为ES5满足通用性呢? 先用这个最常用的Babel的用法来引入吧. 一  首 ...

  10. python进程间数据不共享(示例演示)

    import multiprocessing data_list = [] def task(arg): data_list.append(arg) print(data_list) def run( ...