一、前言

在界面编程中,我们常常会遇到具有依赖性质的ComboBox框,比如最常见的:

省/直辖市 => 地级市/区 => 区/街道

今天就说一下在WPF的MVVM模式中如何实现该功能

二、范例

      假设有一段原始数据,名为:TotalData

其包含四个可以过滤的属性:AType、BType、CType、DType

与其对应的有四个ComboBox:

1. SelectedItem绑定:ATypeSelected   ItemsSource绑定为:ATypes

2. SelectedItem绑定:BTypeSelected   ItemsSource绑定为:BTypes

3. SelectedItem绑定:CTypeSelected   ItemsSource绑定为:CTypes

4. SelectedItem绑定:DTypeSelected   ItemsSource绑定为:DTypes

class Temp
{
private string _ATypeSelected;
public string ATypeSelected
{
get
{
return _ATypeSelected;
}
set
{
if (SetProperty(ref _ATypeSelected, value, () => ATypeSelected))
{
BTypes.Clear();
var BTempTypes = new List<string>(); if (ATypeSelected != "全部A类型" && ATypeSelected != null)
{
BTempTypes.AddRange(TotalData.Where(x => x.AType == ATypeSelected).Select(item => item.BType));
}
else
{
BTempTypes.AddRange(TotalData.Select(item => item.BType));
} BTypes.Add("全部B类型");
foreach (var item in BTempTypes.Distinct())
{
BTypes.Add(item);
}
BTypeSelected = BTypes[];
}
}
} private string _BTypeSelected;
public string BTypeSelected
{
get
{
return _BTypeSelected;
}
set
{
if (SetProperty(ref _BTypeSelected, value, () => BTypeSelected))
{
CTypes.Clear();
var CTempTypes = new List<string>(); if (ATypeSelected != "全部A类型" && ATypeSelected != null)
{
if (BTypeSelected != "全部B类型" && BTypeSelected != null)
{
CTempTypes.AddRange(from item in TotalData where item.AType == ATypeSelected && item.BType == BTypeSelected select item.CType);
}
else if (BTypeSelected == "全部B类型")
{
CTempTypes.AddRange(from item in TotalData where item.AType == ATypeSelected select item.CType);
}
}
else
{
if (BTypeSelected != "全部B类型" && BTypeSelected != null)
{
CTempTypes.AddRange(from item in TotalData where item.BType == BTypeSelected select item.CType);
}
else if (BTypeSelected == "全部B类型")
{
CTempTypes.AddRange(TotalData.Select(item => item.CType));
}
}
CTypes.Add("全部C类型");
foreach (var item in CTempTypes.Distinct())
{
CTypes.Add(item);
}
CTypeSelected = CTypes[];
}
}
} private string _CTypeSelected;
public string CTypeSelected
{
get
{
return _CTypeSelected;
}
set
{
if (SetProperty(ref _CTypeSelected, value, () => CTypeSelected))
{
DTypes.Clear();
var DTempTypes = new List<string>(); if (ATypeSelected != "全部A类型" && ATypeSelected != null)
{
if (BTypeSelected != "全部B类型" && BTypeSelected != null)
{
if (CTypeSelected != null && CTypeSelected != "全部C类型")
{
DTempTypes.AddRange(from item in TotalData where item.BType == BTypeSelected && item.CType == CTypeSelected && item.AType == ATypeSelected select item.DType);
}
else if (CTypeSelected == "全部C类型")
{
DTempTypes.AddRange(from item in TotalData where item.BType == BTypeSelected && item.AType == ATypeSelected select item.DType);
} }
else if (BTypeSelected == "全部B类型")
{
if (CTypeSelected != null && CTypeSelected != "全部C类型")
{
DTempTypes.AddRange(from item in TotalData where item.CType == CTypeSelected && item.AType == ATypeSelected select item.DType);
}
else if (CTypeSelected == "全部C类型")
{
DTempTypes.AddRange(from item in TotalData where item.AType == ATypeSelected select item.DType);
}
}
}
else
{
if (BTypeSelected != "全部B类型" && BTypeSelected != null)
{
if (CTypeSelected != null && CTypeSelected != "全部C类型")
{
DTempTypes.AddRange(from item in TotalData where item.BType== BTypeSelected && item.CType == CTypeSelected select item.DType);
}
else if (CTypeSelected == "全部C类型")
{
DTempTypes.AddRange(from item in TotalData where item.BType == BTypeSelected select item.DType);
} }
else if (BTypeSelected == "全部B类型")
{
if (CTypeSelected != null && CTypeSelected != "全部C类型")
{
DTempTypes.AddRange(from item in TotalData where item.CType == CTypeSelected select item.DType);
}
else if (CTypeSelected == "全部C类型")
{
DTempTypes.AddRange(TotalData.Select(item => item.DType));
}
}
} DTypes.Add("全部D类型");
foreach (var item in DTempTypes.Distinct())
{
DTypes.Add(item);
}
DTypeSelected = DTypes[];
}
}
} private string _DTypeSelected; public string DTypeSelected
{
get { return _DTypeSelected; }
set
{
SetProperty(ref _DTypeSelected, value, () => DTypeSelected);

//在此处可以根据上述的ATypeSelected、BTypeSelected、CTypeSelected、DTypeSelected 对 TotalData 进行过滤从而得到想要显示在界面上的数据列表 }
} }

【MVVM Dev】多个具有依赖性质的ComboBox对数据的过滤的更多相关文章

  1. 【MVVM DEV】DataColumn中的TextBox与ComboBox的并存

    一.前言       在WPF编程中,有时候我们使用DataGrid会需要在一个DataColumn中既有TextBox,也要有ComboBox或者TextBlock等其他数据显示样式. 这个时候我们 ...

  2. 【MVVM Dev】PART_Editor的使用

    一.前言       在日常的界面开发中,我们大多使用MVVM模式进行开发.通常情况下,一个PropertyGridControl或者DataGrid的ItemsSource设置好, 然后每一列绑定好 ...

  3. hdu-3449 Consumer---有依赖性质的背包

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3449 题目大意: fj打算去买一些东西,在那之前,他需要一些盒子去装他打算要买的不同的物品.每一个盒 ...

  4. 【MVVM Dev】ComboBox嵌入CheckBox的显示问题

    一.前言 在ComboBox中嵌入若干个CheckBox时,当我们勾选一些CheckBox,ComboBox会显示相应的勾选项. 例如:CheckBox项有A,B,C   那么勾选这三项,ComboB ...

  5. 不依赖三方库从图像数据中获取宽高-gif、bmp、png、jepg

    int extract_pic_info(const BYTE *pic, const uint32_t size, int &width, int &height) { ; widt ...

  6. 领域驱动和MVVM应用于UWP开发的一些思考

    领域驱动和MVVM应用于UWP开发的一些思考 0x00 起因 有段时间没写博客了,其实最近本来是根据梳理的MSDN上的资料(UWP开发目录整理)有条不紊的进行UWP学习的.学习中有了心得体会或遇到了问 ...

  7. WPF快速入门系列(8)——MVVM快速入门

    一.引言 在前面介绍了WPF一些核心的内容,其中包括WPF布局.依赖属性.路由事件.绑定.命令.资源样式和模板.然而,在WPF还衍生出了一种很好的编程框架,即WVVM,在Web端开发有MVC,在WPF ...

  8. 基于MVVM的知乎日报应用安卓源码

    使用data binding , dagger2 , retrofit2和rxjava实现的,基于MVVM的知乎日报APP运行效果: <ignore_js_op> 使用说明: 项目结构 a ...

  9. 浅析前端开发中的 MVC/MVP/MVVM 模式

    MVC,MVP和MVVM都是常见的软件架构设计模式(Architectural Pattern),它通过分离关注点来改进代码的组织方式.不同于设计模式(Design Pattern),只是为了解决一类 ...

随机推荐

  1. Linux系统基础网络配置老鸟精华篇

    对于linux高手看似简单的网络配置问题,也许要说出所以然来也并不轻松,因此仍然有太多的初学者徘徊在门外就不奇怪了,这里,老男孩老师花了一些时间总结了这个文档小结,也还不够完善,欢迎大家补充,交流.谢 ...

  2. 搜索引擎ElasticSearch系列(一): ElasticSearch2.4.4环境搭建

    一:ElasticSearch简介 Elasticsearch is a distributed, RESTful search and analytics engine capable of sol ...

  3. CentOS7.x安装Docker1.11.1

    原文发表于cu:2016-05-30 本文属于重发,当前Docker已经分为EE与CE版本,CE版本是17.06.0-ce,最新的CE版本安装方式已略有不同:但可以指定安装版本,如1.11.1,1.1 ...

  4. Gradle初使用

    我以前一直使用Maven来构建工程,这两天突然发现gradle也非常好用,记录一下自己使用gradle的过程. Gradle的下载与配置 本次选择下载的是gradle3.5版本,没选最新的gradle ...

  5. leetcode个人题解——#31 Next Permutation

    写这题时脑子比较混乱,重写了一遍wiki大佬的解法. 算法: According to Wikipedia, a man named Narayana Pandita presented the fo ...

  6. 亚马逊中国耳机巨头Jabra官方旗舰店上线

    日前,亚马逊中国(Z.cn)宣布,全球无线技术顶级领导品牌 Jabra (捷波朗)官方旗舰店正式上线,产品品类涵盖蓝牙耳机.音乐耳机.无线音箱和车载系列产品.Jabra 旗舰店上线伊始便汇集了 60 ...

  7. 全国城市一卡通一级TSM平台业务架构及意义

    [导读]TSM平台是一种具有鲜明行业属性的平台,因此,各行业都建立了本行业的TSM平台.为促进城市一卡通行业移动支付的快速发展,住房和城乡建设部也建立了全国城市一卡通行业一级TSM平台. 作为住建部标 ...

  8. 【探路者】团队中的每一次感动——Alpha版

    我是[探路者]团队的leader翟宇豪.在软件工程课程开始时,当听说有团队作业这个任务时,我个人还是对leader这个角色很期待的.我很希望通过自己的努力,让我所在的团队变得更好,让组里的每一个成员在 ...

  9. "助成"招聘网站视频简介

    我们小组为我们的作品录制了一个一分多钟的电梯介绍视频,这是视频连接,我上传到了优酷上:http://v.youku.com/v_show/id_XMzIzMTc1ODc2NA==.html?spm=a ...

  10. c# apache服务器请求得到数据(初级)

    1.代码: string data = new WebClient().DownloadString("http://localhost:81/123.txt");