【MVVM Dev】多个具有依赖性质的ComboBox对数据的过滤
一、前言
在界面编程中,我们常常会遇到具有依赖性质的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对数据的过滤的更多相关文章
- 【MVVM DEV】DataColumn中的TextBox与ComboBox的并存
一.前言 在WPF编程中,有时候我们使用DataGrid会需要在一个DataColumn中既有TextBox,也要有ComboBox或者TextBlock等其他数据显示样式. 这个时候我们 ...
- 【MVVM Dev】PART_Editor的使用
一.前言 在日常的界面开发中,我们大多使用MVVM模式进行开发.通常情况下,一个PropertyGridControl或者DataGrid的ItemsSource设置好, 然后每一列绑定好 ...
- hdu-3449 Consumer---有依赖性质的背包
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3449 题目大意: fj打算去买一些东西,在那之前,他需要一些盒子去装他打算要买的不同的物品.每一个盒 ...
- 【MVVM Dev】ComboBox嵌入CheckBox的显示问题
一.前言 在ComboBox中嵌入若干个CheckBox时,当我们勾选一些CheckBox,ComboBox会显示相应的勾选项. 例如:CheckBox项有A,B,C 那么勾选这三项,ComboB ...
- 不依赖三方库从图像数据中获取宽高-gif、bmp、png、jepg
int extract_pic_info(const BYTE *pic, const uint32_t size, int &width, int &height) { ; widt ...
- 领域驱动和MVVM应用于UWP开发的一些思考
领域驱动和MVVM应用于UWP开发的一些思考 0x00 起因 有段时间没写博客了,其实最近本来是根据梳理的MSDN上的资料(UWP开发目录整理)有条不紊的进行UWP学习的.学习中有了心得体会或遇到了问 ...
- WPF快速入门系列(8)——MVVM快速入门
一.引言 在前面介绍了WPF一些核心的内容,其中包括WPF布局.依赖属性.路由事件.绑定.命令.资源样式和模板.然而,在WPF还衍生出了一种很好的编程框架,即WVVM,在Web端开发有MVC,在WPF ...
- 基于MVVM的知乎日报应用安卓源码
使用data binding , dagger2 , retrofit2和rxjava实现的,基于MVVM的知乎日报APP运行效果: <ignore_js_op> 使用说明: 项目结构 a ...
- 浅析前端开发中的 MVC/MVP/MVVM 模式
MVC,MVP和MVVM都是常见的软件架构设计模式(Architectural Pattern),它通过分离关注点来改进代码的组织方式.不同于设计模式(Design Pattern),只是为了解决一类 ...
随机推荐
- html学习第一天
由于之后想做个网站,所以web前端的也要学习一下. 昨天看了一下html,今天做一下记录. 首先是安装工具,用文本编辑器有点麻烦,我选择的是强大的 Dreamweaver CS6,不过大家喜欢文本编辑 ...
- Android开发笔记——视频录制播放常见问题
本文分享自己在视频录制播放过程中遇到的一些问题,主要包括: 视频录制流程 视频预览及SurfaceHolder 视频清晰度及文件大小 视频文件旋转 一.视频录制流程 以微信为例,其录制触发为按下(住) ...
- dotweb now released to Version 1.5
dotweb released to Version 1.5!!https://github.com/devfeel/dotweb What's new? 重要:go版本适配升级为1.9+ New f ...
- APP端测试,常见功能测试点汇总
除去每个产品和版本不同的业务需求以及功能,针对于大多数的APP的共同点和移动设备的特性,本文总结了一些APP功能测试中经常遇见,需要考虑到的测试点以共参考 一.安装和卸载 应用的安装和卸载在任何一款A ...
- selenium +java 多个类公用driver问题
问题点:太久没有写selenium代码,居然把driver公用的问题忘记了,即:每写一个测试类,执行过程中都会新建一个窗口,这样应该说是非常不专业的. 大概想了一个方法,虽然看起来也不怎么专业,但感觉 ...
- mysql 转换13位数字毫秒时间
MySQL毫秒值和日期转换,MYSQL内置函数FROM_UNIXTIME: select FROM_UNIXTIME(t.createDate/1000,'%Y-%m-%d %h:%i:%s') as ...
- ES6的新特性(11)——Class 的继承
Class 的继承 简介 Class 可以通过extends关键字实现继承,这比 ES5 的通过修改原型链实现继承,要清晰和方便很多. class Point { } class ColorPoint ...
- moment.js学习总结
一. 介绍: moment.js不依赖任何第三方库,支持字符串.Date.时间戳以及数组等格式,可以像PHP的date()函数一样,格式化日期时间,计算相对时间,获取特定时间后的日期时间等等.下面是一 ...
- Python中用字符串导入module
在Python中,无法通过字符串来导入一个module文件: import "string" # Error x = "string" import x # 不 ...
- AngularJS学习之数据绑定
既然AngularJS是以数据作为驱动的MVC框架,在上一篇文章中,也介绍了AngularJS如何实现MVC模式的,所有模型里面的数据,都必须经过控制器,才能展示到视图中. 什么是数据绑定 首先来回忆 ...