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. XHTML XML

    XHTML是可扩展的超文本标记语言(Extensible HyperText Markup Language). l  XHTML是w3c组织在2000年的时候为了增强HTML推出的,本来是想替代HT ...

  2. word中使用MathType能做什么

    在Office中写论文,特别是一些比较专业的论文需要用到各种公式的.会发现有很多地方Office自带的公式编辑器都无法完成,所以要用到MathType公式编辑器这个好用的工具了.MathType是一款 ...

  3. python3----练习题(过滑块验证)

    # 导入模块 from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webd ...

  4. Windows下将gvim8配置为Python IDE

    目录 1.准备工作 2.安装 3.配置 _vimrc 4.编写和编译运行程序 正文 Windows下将gvim配置为Python IDE 回到顶部 1.准备工作 将下面的安装包或者文件下载好 1) P ...

  5. jupyter 修改工作路径

    在所需打开的目录中新建一个runJupyter.bat文件 将内容修改为: cd ......jupyter notebook 注1:上述两行中,第一行的......为路径(可以不添加,可空着不填), ...

  6. 教程less

    http://lesscss.cn/features/ Overview As an extension to CSS, Less is not only backwards compatible w ...

  7. C++名人的网站 转

    正如我们可以通过计算机历史上的重要人物了解计算机史的发展,C++相关人物的网站也可以使我们得到最有价值的参考与借鉴. 正如我们可以通过计算机历史上的重要人物了解计算机史的发展,C++相关人物的网站也可 ...

  8. 微软MVP Round Table

    2017年7月7日,微软VS圈子的老大兼女神Julia(潘正磊)以及Peter Hu等人,和若干MVP一起在进行了一次Round Table讨论. 讨论过程中主要针对VS和TFS/VSTS相关的功能. ...

  9. crash处理core文件

    (一时心血来潮总结的,供大家参考,时间仓促,不足之处勿拍砖,欢迎讨论~)Crash工具用于解析Vmcore文件,Vmcore文件为通过kdump等手段收集的操作系统core dump信息,在不采用压缩 ...

  10. ESX 5.0 上运行虚拟ESX

    如何在ESX上安裝ESX需注意幾點: 1.Guest OS選Linux / Red Hat Enterprise Linux 5 (64-bit) 2.使用SSH連線實體ESX主機下指令 添加*.vm ...