【WPF】城市级联(XmlDataProvider)
首先在绑定的时候进行转换:
- public class RegionConverter : IValueConverter
- {
- public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
- {
- var name = value as string;
- var filter = parameter as string;
- if (string.IsNullOrEmpty(name) && filter != "country")
- {
- return null;
- }
- var provider = new XmlDataProvider();
- provider.Source = new Uri("Resources/Region.xml", UriKind.Relative);
- if (filter == "country")
- {
- provider.XPath = "/region/country/@name";
- }
- else if (filter == "province")
- {
- provider.XPath = string.Format("/region/country[@name='{0}']/province/@name", name);
- }
- else if (filter == "city")
- {
- provider.XPath = string.Format("/region/country/province[@name='{0}']/city/@name", name);
- }
- else if (filter == "town")
- {
- provider.XPath = string.Format("/region/country/province/city[@name='{0}']/town/@name", name);
- }
- return provider;
- }
- public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
- {
- throw new NotImplementedException();
- }
- }
再看以下如何绑定的
- <converters:RegionConverter x:Key="region"/>
- <ComboBox Grid.Column="" x:Name="country"
- DataContext="{Binding Converter={StaticResource region}, ConverterParameter=country}"
- SelectedValue="{Binding DataContext.CurrEditorItem.Country,UpdateSourceTrigger=PropertyChanged,
- RelativeSource={RelativeSource AncestorType={x:Type mui:ModernWindow}}}"
- ItemsSource="{Binding}"
- Width="" Style="{StaticResource CommonComboBoxStyle}" />
- <ComboBox Grid.Column="" x:Name="province"
- DataContext="{Binding SelectedValue, ElementName=country, Converter={StaticResource region}, ConverterParameter=province}"
- SelectedValue="{Binding DataContext.CurrEditorItem.Province,UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource AncestorType={x:Type mui:ModernWindow}}}"
- ItemsSource="{Binding}"
- Width="" Style="{StaticResource CommonComboBoxStyle}" />
- <ComboBox Grid.Column="" x:Name="city"
- DataContext="{Binding SelectedValue, ElementName=province, Converter={StaticResource region}, ConverterParameter=city}"
- SelectedValue="{Binding DataContext.CurrEditorItem.City,UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource AncestorType={x:Type mui:ModernWindow}}}"
- ItemsSource="{Binding}"
- Width="" Style="{StaticResource CommonComboBoxStyle}" />
- <ComboBox Grid.Column="" x:Name="town"
- DataContext="{Binding SelectedValue, ElementName=city, Converter={StaticResource region}, ConverterParameter=town}"
- SelectedValue="{Binding DataContext.CurrEditorItem.Area,UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource AncestorType={x:Type mui:ModernWindow}}}"
- ItemsSource="{Binding}" Text="{Binding Area,UpdateSourceTrigger=PropertyChanged}"
- Width="" Style="{StaticResource CommonComboBoxStyle}" />
- <TextBlock Grid.Column="" Text="国家" Tag="{Binding SelectedValue, ElementName=country}"
- Style="{StaticResource TipTextBlock}"/>
- <TextBlock Grid.Column="" Text="省份" Tag="{Binding SelectedValue, ElementName=province}"
- Style="{StaticResource TipTextBlock}"/>
- <TextBlock Grid.Column="" Text="市/区" Tag="{Binding SelectedValue, ElementName=city}"
- Style="{StaticResource TipTextBlock}"/>
- <TextBlock Grid.Column="" Text="县/镇" Tag="{Binding SelectedValue, ElementName=town}"
- Style="{StaticResource TipTextBlock}"/>
TextBlock放在ComboBox上面,Textblock样式如下
- <Style x:Key="TipTextBlock" TargetType="{x:Type TextBlock}">
- <Setter Property="IsHitTestVisible" Value="False" />
- <Setter Property="HorizontalAlignment" Value="Left"/>
- <Setter Property="VerticalAlignment" Value="Center"/>
- <Setter Property="Margin" Value="12,0,0,0"/>
- <Setter Property="Opacity" Value=""/>
- <Style.Triggers>
- <Trigger Property="Tag" Value="{x:Null}">
- <Setter Property="Opacity" Value=""/>
- </Trigger>
- </Style.Triggers>
- </Style>
枚举在WPF的应用:
- <ComboBox x:Name="cbbDataType" ItemsSource="{Binding Source={StaticResource InfoDetailTypeItems}}"
- SelectedItem="{Binding CurrEditorItem.DataType, ValidatesOnDataErrors=True}"
- ItemTemplate="{StaticResource InfoDetailTypeDataTemplate}"
- Grid.Row="" Grid.Column=""
- Style="{StaticResource EditorComboBoxStyle}" />
- <x:Array x:Key="InfoDetailTypeItems" Type="{x:Type adservice:ShowDataType}">
- <adservice:ShowDataType>Image</adservice:ShowDataType>
- <adservice:ShowDataType>Video</adservice:ShowDataType>
- <adservice:ShowDataType>ThreeDModel</adservice:ShowDataType>
- </x:Array>
- public ObservableCollection<Employee> userList = new ObservableCollection<Employee>();
- public MainWindow()
- {
- InitializeComponent();
- this.List.ItemsSource = userList;
- ThreadPool.QueueUserWorkItem((m) =>
- {
- string myfiles = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
- var query = from pathname in Directory.GetFiles(myfiles) select new { pathtest = pathname };
- foreach (var item in query)
- {
- this.Dispatcher.BeginInvoke(new Action(() =>
- {
- userList.Add(new Employee { Name = item.pathtest });
- }));
- Thread.Sleep(100);
- }
- });
- }
- private IList<CategoryTreeDataModel> GenerateTree(IEnumerable<CategoryTreeDataModel> list)
- {
- IDictionary<int, IList<CategoryTreeDataModel>> childDict = new Dictionary<int, IList<CategoryTreeDataModel>>();
- // 生成父子关系字典
- foreach (var item in list)
- {
- if (!childDict.ContainsKey(item.ParentId))
- childDict.Add(item.ParentId, new List<CategoryTreeDataModel>());
- var tempList = childDict[item.ParentId];
- tempList.Add(item);
- }
- if (!childDict.ContainsKey(0)) return null;
- // 生成TreeModel
- foreach (var itemList in childDict.Values)
- {
- foreach (var item in itemList)
- {
- var parentId = item.CategoryId;
- item.Children = childDict.ContainsKey(parentId) ? childDict[parentId] : null;
- }
- }
- return GenerateListTree(childDict[0], 0, childDict).ToList();
- }
- /// <summary>
- /// 生成拥有子级关系的列表
- /// </summary>
- /// <param name="list">父级列表</param>
- /// <param name="level">树的层级</param>
- /// <param name="childDict">父子关系字典</param>
- private IEnumerable<CategoryTreeDataModel> GenerateListTree(IEnumerable<CategoryTreeDataModel> list, int level, IDictionary<int, IList<CategoryTreeDataModel>> childDict)
- {
- if (list == null) yield break;
- foreach (var item in list)
- {
- yield return item;
- if (!childDict.ContainsKey(item.CategoryId)) continue;
- foreach (var childItem in GenerateListTree(childDict[item.CategoryId], level + 1, childDict))
- {
- yield return childItem;
- }
- }
- }
【WPF】城市级联(XmlDataProvider)的更多相关文章
- iOS:城市级联列表的使用
1.介绍: 现在越来越多的项目都用到了地址,尤其是电商O2O的购物平台,我之前做的教育产品和电商产品都用到了,而实现地址的设置用到的技术就是城市级联列表,即普遍的做法就是自定义选择器控件UIPicke ...
- GeneXus笔记本—城市级联下拉
最近在交流GeneXus的时候 总是会遇到有城市级联下拉的问题 这里就简单做几种方式 供大家参考参考 第一种就是直接绑定关联信息然后在后者的条件模块设定条件即可 具体如下: 首先我们所需要的表为pro ...
- WPF:ComboBox使用XmlDataProvider做级联
程序功能: 使用ComboBox做级联,数据源为XML文件,适合小数据量呈现 程序代码: <Window x:Class="WpfApplication1.LayouTest" ...
- WPF:在XmlDataProvider上使用主-从绑定(Master-Detail Binding)
原文 http://www.cnblogs.com/mgen/archive/2011/06/19/2084553.html 示例程序: 如上程序截图,一目了然典型的主从模式绑定应用,如果里面的数据不 ...
- Linq 单表城市级联
var list = (from province in db.Areas && province.IsDel == join city in db.Areas on province ...
- WPF:数据和行为
如果自己来做一个UI框架,我们会首先关注哪些方面?我想UI框架主要处理的一定包括两个主要层次的内容,一个是数据展现,另一个就是数据操作,所以UI框架必须能够接收各种不同的数据并通过UI界面展现出来,然 ...
- WPF的本质:数据和行为
如果自己来做一个UI框架,我们会首先关注哪些方面?我想UI框架主要处理的一定包括两个主要层次的内容,一个是数据展现,另一个就是数据操作,所以UI框架必须能够接收各种不同的数据并通过UI界面展现出来,然 ...
- jquery插件课程1 幻灯片、城市选择、日期时间选择、拖放、方向拖动插件
jquery插件课程1 幻灯片.城市选择.日期时间选择.拖放.方向拖动插件 一.总结 一句话总结:都是jquery插件,都还比较小,参数(配置参数.数据)一般都是通过json传递. 1.插件配置数据 ...
- C#源码500份
C Sharp 短信发送平台源代码.rar http://1000eb.com/5c6vASP.NET+AJAX基础示例 视频教程 http://1000eb.com/89jcC# Winform ...
随机推荐
- 二型错误和功效(Type II Errors and Test Power)
sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&am ...
- vue-router的新奇写法
加班中........................... 我们以前写路由是下面这样的 这导致了页面一多,我们的路由文件内容就比较多,不好看. 下面我为大家介绍一下,新的一种写法 这种写法,我们只需 ...
- hdu4699-Editor
Sample Input 8 I 2 I -1 I 1 Q 3 L D R Q 2 Sample Output 2 3 发现IDLR四种操作都在光标处发生,且操作完成后光标至多移动1个位置,根据这种“ ...
- leetcode 刷题日志 2018-03-26
58. 最后一个单词的长度 分析:找最后一个非空格,向前找 int lengthOfLastWord(string s) { int i = s.find_last_not_of(' '); int ...
- 基础但是很重要的2-sat POJ 3678
http://poj.org/problem?id=3678 题目大意:就是给你n个点,m条边,每个点都可以取值为0或者1,边上都会有一个符号op(op=xor or and三种)和一个权值c.然后问 ...
- POJ3061 Subsequence 尺取or二分
Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...
- oozie与sqoop的简单案例
1:拷贝模板 2:拷贝hive用的jar包 方式一: 3:编辑job.properties # # Licensed to the Apache Software Foundation (ASF) u ...
- 用C++写一个没人用的ECS
github地址:https://github.com/yangrc1234/Resecs 在做大作业的时候自己实现了一个简单的ECS,起了个名字叫Resecs. 这里提一下一些实现的细节,作为回顾. ...
- Xcode下 gdb 调试命令
Xcode的调试器为用户提供了一个GDB的图形化界面,GDB是GNU组织的开放源代码调试器.您可以在Xcode的图形界面里做任何事情:但是,如果您需要您可以在命令行里使用GDB的命令,且gdb可以在终 ...
- Linux实用命令之git-svn
近日发现了有一个工具,git-svn,可以打通git svn之间的鸿沟. 很适合习惯于git,却需要维护svn代码的同学. 安装 sudo apt-get install git-svn 具体使用就不 ...