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. 网络协议之ts---ts 流分析详解

    名词缩写翻译: es:elements stream 基本流 pes:packet elements stream 打包流 pts:presentation time stamp 显示时间戳 dts: ...

  2. 上下居中css

    .css{ position: relative, top: 50%, transform: translateY(-50%) }

  3. gcc/g++ 实战之编译的四个过程

    gcc和g++分别是GNU(一个开源组织)的c&c++编译器   对于.c后缀的文件,gcc把它当做是C程序,g++当做是C++程序:对于.cpp后缀的文件,gcc和g++都会当做c++程序. ...

  4. boost实用工具:assign库了解学习

    许多时候,我们需要为容器初始化或者赋值,填入大量的数据; STL容器仅提供了容纳这些数据的方法,但是填充的步骤是相当地麻烦(insert.push_back); 于是,boost::assign出现了 ...

  5. HBase的多节点集群详细启动步骤(3或5节点)(分为Zookeeper自带还是外装)

    HBase的多节点集群详细启动步骤(3或5节点)分为: 1.HBASE_MANAGES_ZK的默认值是false(zookeeper外装)(推荐) 2.HBASE_MANAGES_ZK的默认值是tru ...

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

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

  7. iOS开发之--蓝牙开发实战

    转载自:http://www.cnblogs.com/zyjzyj/p/6029968.html ,感谢英杰 前言 最近一直在开发关于蓝牙的功能,本来是不想写这一篇文章,因为网上关于ios蓝牙开发的文 ...

  8. 面试题思考: 什么是事务(ACID)?

    事务(Transaction)是由一系列对系统中数据进行访问与更新的操作所组成的一个程序 执行逻辑单元(Unit). 狭义上的事务特指数据库事务.一方面,当多个应用程序并发访问数据库时,事务可以在这些 ...

  9. Android中流式布局和热门标签

    1.流式布局特点.应用场景.2.自定义ViewGroup (1)onMeasure:测量子View的宽和高,设置自己的宽和高. (2)onLayout:设置子View的位置. onMeasure:根据 ...

  10. c++ 类声明

    class B; struct A { B* ptr; }; class B { public: }; int main() { ; } A中定义了B的指针,所以要声明class B,在定义处于不完整 ...