C# WPF DataGrid控件实现三级联动
利用DataGrid控件实现联动的功能,在数据库客户软件中是随处可见的,然而网上的资料却是少之又少,令人崩溃。
本篇博文将介绍利用DataGrid控件模板定义的三个ComboBox实现“省、市、区”的三级联动。步骤如下:
一.定义地域信息类(注意包含System.ComponentModel命名空间)
- class RegionInfo : INotifyPropertyChanged //地区信息
- {
- private string _province;//省
- private string _city;//市
- private string _area;//区
- public event PropertyChangedEventHandler PropertyChanged;
- public string Province
- {
- get { return _province; }
- set
- {
- _province = value;
- PropertyChanged(this, new PropertyChangedEventArgs("Province"));
- }
- }
- public string City
- {
- get { return _city; }
- set
- {
- _city = value;
- PropertyChanged(this, new PropertyChangedEventArgs("City"));
- }
- }
- public string Area
- {
- get { return _area; }
- set
- {
- _area = value;
- PropertyChanged(this, new PropertyChangedEventArgs("Area"));
- }
- }
- public RegionInfo(string province, string city, string area)//构造函数
- {
- _province = province;
- _city = city;
- _area = area;
- }
- }
二.编写DataGrid控件的XAML代码
- <!--AutoGenerateColumns="False"这句话告诉控件不自动生成列,是必要的,如果没有,控件会更具数据源自动生成列,这个在我们这种写法中看起来就有重复的两列数据-->
- <DataGrid x:Name="dataGrid" ItemsSource="{Binding}" AutoGenerateColumns="False" HorizontalAlignment="Left" VerticalAlignment="Top" Height="319" Width="302">
- <DataGrid.Columns>
- <!--省-->
- <DataGridTemplateColumn Header="省" Width="100">
- <!--显示模式-->
- <DataGridTemplateColumn.CellTemplate>
- <DataTemplate>
- <TextBlock Text="{Binding Path=Province ,Mode=TwoWay}"></TextBlock>
- </DataTemplate>
- </DataGridTemplateColumn.CellTemplate>
- <!--编辑模式-->
- <DataGridTemplateColumn.CellEditingTemplate>
- <DataTemplate>
- <ComboBox x:Name="ComboBoxProvince" DropDownClosed="ProvinceDropDownClosed" Loaded="ProvinceLoaded" Text="{Binding Path=Province ,Mode=TwoWay}" DisplayMemberPath="Province" ></ComboBox>
- </DataTemplate>
- </DataGridTemplateColumn.CellEditingTemplate>
- </DataGridTemplateColumn>
- <!--市-->
- <DataGridTemplateColumn Header="市" Width="100">
- <!--显示模式-->
- <DataGridTemplateColumn.CellTemplate>
- <DataTemplate>
- <TextBlock Text="{Binding Path=City ,Mode=TwoWay}"></TextBlock>
- </DataTemplate>
- </DataGridTemplateColumn.CellTemplate>
- <!--编辑模式-->
- <DataGridTemplateColumn.CellEditingTemplate>
- <DataTemplate>
- <ComboBox x:Name="ComboBoxCity" DropDownClosed="CityDropDownClosed" Loaded="CityLoaded" Text="{Binding Path=City,Mode=TwoWay}" DisplayMemberPath="City" ></ComboBox>
- </DataTemplate>
- </DataGridTemplateColumn.CellEditingTemplate>
- </DataGridTemplateColumn>
- <!--区-->
- <DataGridTemplateColumn Header="区" Width="100">
- <!--显示模式-->
- <DataGridTemplateColumn.CellTemplate>
- <DataTemplate>
- <TextBlock Text="{Binding Path=Area}"></TextBlock>
- </DataTemplate>
- </DataGridTemplateColumn.CellTemplate>
- <!--编辑模式-->
- <DataGridTemplateColumn.CellEditingTemplate>
- <DataTemplate>
- <ComboBox x:Name="ComboBoxArea" DropDownClosed="AreaDropDownClosed" Loaded="AreaLoaded" Text="{Binding Path=Area}" DisplayMemberPath="Area" ></ComboBox>
- </DataTemplate>
- </DataGridTemplateColumn.CellEditingTemplate>
- </DataGridTemplateColumn>
- </DataGrid.Columns>
- </DataGrid>
三.为DataGrid控件准备数据源对象
对象声明
- ObservableCollection<RegionInfo> regionInfoList = new ObservableCollection<RegionInfo>();//DataGrid的数据源
对象添加
- //DataGrid初始绑定数据
- regionInfoList.Add(new RegionInfo("广东省", "深圳市", "罗湖区"));
- regionInfoList.Add(new RegionInfo("广东省", "深圳市", "南山区"));
对象绑定
- dataGrid.ItemsSource = regionInfoList;//绑定数据源
四.为DataGrid控件的模板列加载时提供选项
对象声明
- ObservableCollection<RegionInfo> regionInfoSelectList = new ObservableCollection<RegionInfo>();//用于DataGrid的模板列加载时提供选项
对象添加
- //三级联动数据项
- regionInfoSelectList.Add(new RegionInfo("广东省", "深圳市", "罗湖区"));
- regionInfoSelectList.Add(new RegionInfo("广东省", "深圳市", "南山区"));
- regionInfoSelectList.Add(new RegionInfo("广东省", "潮州市", "湘桥区"));
- regionInfoSelectList.Add(new RegionInfo("广东省", "潮州市", "枫溪区"));
- regionInfoSelectList.Add(new RegionInfo("湖北省", "武汉市", "江夏区"));
- regionInfoSelectList.Add(new RegionInfo("湖北省", "武汉市", "武昌区"));
五.在DataGrid模板列加载的时候实时更新其数据源并绑定到ComboBox的选项中
- /// <summary>
- /// ProvinceLoaded 省份下拉列表框初始化,绑定数据源
- /// </summary>
- void ProvinceLoaded(object sender, RoutedEventArgs e)
- {
- ComboBox curComboBox = sender as ComboBox;
- //为下拉控件绑定数据源,并选择原选项为默认选项
- string text = curComboBox.Text;
- //去除重复项查找,跟数据库连接时可以让数据库来实现
- var query = regionInfoSelectList.GroupBy(p => p.Province).Select(p => new { Province = p.FirstOrDefault().Province });
- int itemcount = 0;
- curComboBox.SelectedIndex = itemcount;
- foreach (var item in query.ToList())
- {
- if (item.Province == text)
- {
- curComboBox.SelectedIndex = itemcount;
- break;
- }
- itemcount++;
- }
- curComboBox.ItemsSource = query;
- curComboBox.IsDropDownOpen = true;//获得焦点后下拉
- }
- /// <summary>
- /// CityLoaded 市下拉列表框初始化,绑定数据源
- /// </summary>
- void CityLoaded(object sender, RoutedEventArgs e)
- {
- //获得当前选中项的省份信息
- string province = (dataGrid.SelectedItem as RegionInfo).Province;
- //查找选中省份下的市作为数据源
- var query = (from l in regionInfoSelectList
- where (l.Province == province)
- group l by l.City into grouped
- select new { City = grouped.Key });
- ComboBox curComboBox = sender as ComboBox;
- //为下拉控件绑定数据源,并选择原选项为默认选项
- string text = curComboBox.Text;
- //去除重复项查找,跟数据库连接时可以让数据库来实现
- int itemcount = 0;
- curComboBox.SelectedIndex = itemcount;
- foreach (var item in query.ToList())
- {
- if (item.City == text)
- {
- curComboBox.SelectedIndex = itemcount;
- break;
- }
- itemcount++;
- }
- curComboBox.ItemsSource = query;
- curComboBox.IsDropDownOpen = true;//获得焦点后下拉
- }
- /// <summary>
- /// AreaLoaded 区下拉列表框初始化,绑定数据源
- /// </summary>
- void AreaLoaded(object sender, RoutedEventArgs e)
- {
- string province = (dataGrid.SelectedItem as RegionInfo).Province;
- string city = (dataGrid.SelectedItem as RegionInfo).City;
- //查找选中省份下的市作为数据源
- var query = (from l in regionInfoSelectList
- where (l.Province == province && l.City == city)
- group l by l.Area into grouped
- select new { Area = grouped.Key });
- ComboBox curComboBox = sender as ComboBox;
- //为下拉控件绑定数据源,并选择原选项为默认选项
- string text = curComboBox.Text;
- //去除重复项查找,跟数据库连接时可以让数据库来实现
- int itemcount = 0;
- curComboBox.SelectedIndex = itemcount;
- foreach (var item in query.ToList())
- {
- if (item.Area == text)
- {
- curComboBox.SelectedIndex = itemcount;
- break;
- }
- itemcount++;
- }
- curComboBox.ItemsSource = query;
- curComboBox.IsDropDownOpen = true;//获得焦点后下拉
- }
做到这一步基本上算是做完了,但是编译运行后会发现在选择省或者市的时候,后面的选项并没有做相应的改变,这是上一篇文章 C# WPF DataGrid控件同行编辑的实时更新问题 所述的问题,所以还需最后一步
六.参考 C# WPF DataGrid控件同行编辑的实时更新问题 解决更新问题(懒得再写一遍了哈哈)
哦哦,更新代码分别写在
- /// <summary>
- /// CityDropDownClosed 市下拉列表框选择改变刷新
- /// </summary>
- private void CityDropDownClosed(object sender, EventArgs e)
- {
- }
- /// <summary>
- /// ProvinceDropDownClosed 省份下拉列表框选择改变刷新
- /// </summary>
- private void ProvinceDropDownClosed(object sender, EventArgs e)
- {
- }
- /// <summary>
- /// AreaDropDownClosed 区下拉列表框选择改变刷新
- /// </summary>
- private void AreaDropDownClosed(object sender, EventArgs e)
- {
- }
这三个函数中。如果还想再只能点,在省下拉列表选择后,市下拉列表下拉。市下拉列表选择后,区下拉列表下拉,则可以参考 C# WPF 模拟键盘输入与UI控件进行交互 这篇文章,然后在上面提到的省、市两个下拉列表框中模拟键盘按下“TAD”键即可。
C# WPF DataGrid控件实现三级联动的更多相关文章
- WPF DataGrid 控件的运用
WPF DataGrid 控件的运用 运行环境:Window7 64bit,.NetFramework4.61,C# 6.0: 编者:乌龙哈里 2017-02-23 参考: King Cobra 博客 ...
- NPOI导出WPF DataGrid控件显示数据
最近做个项目,需要导出DataGrid显示的数据,中间遇到了不少的坑,在此纪录一下,方便以后查看,也希望能给用到的人,一点帮助. 导出DataGrid显示的数据,并不是导出DataGrid的Items ...
- timer控件、三级联动
timer控件: 实现时间日期自增长: using System; using System.Collections.Generic; using System.ComponentModel; usi ...
- 【2017-05-05】timer控件、三级联动、帐号激活权限设置
一.Timer控件 Timer实际就是一个线程控件. 属性:Enabled 是否被启用 Interval 多长时间执行一次控件中的代码 事件: Tick 事件中放要执行的代码. ...
- timer控件、三级联动、帐号激活权限设置
一.Timer控件 Timer实际就是一个线程控件. 属性:Enabled 是否被启用 Interval 多长时间执行一次控件中的代码 事件: Tick 事件中放要执行的代码. ...
- WPF DataGrid控件中某一列根据另一个文本列的值显示相应的模板控件
之前做项目的时候需要实现这样一个功能.WPF DataGrid有两列,一列为"更新状态”列,一列为"值"列,如果"更新状态"列的值为“固定值更新”,则 ...
- WPF:获取DataGrid控件单元格DataGridCell
转载:http://blog.csdn.net/jhqin/article/details/7645357 /* ------------------------------------------- ...
- wpf研究之道-datagrid控件(1)
"想要说些什么 又不知从何说起",每当想要写一些关于wpf的文章,总是沉思良久,怕自己写不好.今天我想要说的是wpf中datagrid控件.我们先来看看它在整个类的层次结构: ...
- wpf研究之道——datagrid控件分页
这是我们的datagrid分页效果图,有上一页,下一页,可以跳到任何一页.当页码比较多的时候,只显示几页,其余用点点,界面实现如下: <!--分页--> <StackPanel Or ...
随机推荐
- 哈工大CSAPP大作业
第1章 概述 1.1 Hello简介 hello的源码hello.c文件,要生成可执行文件,首先要进行预处理,其次要进行编译生成汇编代码,接着进行汇编处理生成目标文件,目标文件通过链接器形成一个可执行 ...
- json键的不能像值一样拼写的问题
今天碰到了一个json的键不能拼写的问题 解决方法是 先把json对象作为一个字符串拼写 然后再通过eavl函数转为json对象 $(".select_date").each( ...
- Swoole 简单学习
Swoole 百度百科:是一个PHP扩展,扩展不是为了提升网站的性能,是为了提升网站的开发效率.最少的性能损耗,换取最大 的开发效率.利用Swoole扩展,开发一个复杂的Web功能,可以在很短的时间内 ...
- jquery鼠标点击窗口或浮动层以外关闭层【阻止冒泡事件】
$(".up-list a.th1").click(function(){ $(this).next("ul#up-list-ul").show(); }); ...
- 牛客网 Wannafly挑战赛5 B.可编程拖拉机比赛-ceil()函数+floor()函数
可编程拖拉机比赛 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 65536K,其他语言131072K64bit IO Format: %lld 题目描述 “这个比赛,归根结底就是控制一 ...
- [开源] FreeSql.Tools Razor 生成器
FreeSql 经过半年的开发和坚持维护,在 0.6.x 版本中完成了几大重要事件: 1.按小包拆分,每个数据库实现为单独 dll: 2.实现 .net framework 4.5 支持: 3.同时支 ...
- k8s之pod控制器
1.生产中,很少会跑一个自主式pod,一般由控制器去创建pod,其配置文件中内嵌了pod的创建方式. pod控制器:ReplicaSet.Deployment.DaemonSet.Job.Cronjo ...
- PyTorch学习笔记之初识word_embedding
import torch import torch.nn as nn from torch.autograd import Variable word2id = {'hello': 0, 'world ...
- How to Use Dtrace Tracing Ruby Executing
http://googya.github.io/blog/categories/dtrace/ 最近看了点关于Dtrace的东西,它是个通用型的工具,但我主要集中于分析ruby程序的执行上面.关于操作 ...
- SilverLight:基础控件使用(4)-日期显示和选择类控件
ylbtech-SilverLight-Basic-Control:基础控件使用(4)-日期显示和选择类控件 Calendar,DatePicker 1.A,返回顶部 Calendar控件(日期控件) ...