首先在绑定的时候进行转换:

  1. public class RegionConverter : IValueConverter
  2. {
  3. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  4. {
  5. var name = value as string;
  6. var filter = parameter as string;
  7. if (string.IsNullOrEmpty(name) && filter != "country")
  8. {
  9. return null;
  10. }
  11. var provider = new XmlDataProvider();
  12. provider.Source = new Uri("Resources/Region.xml", UriKind.Relative);
  13. if (filter == "country")
  14. {
  15. provider.XPath = "/region/country/@name";
  16. }
  17. else if (filter == "province")
  18. {
  19. provider.XPath = string.Format("/region/country[@name='{0}']/province/@name", name);
  20. }
  21. else if (filter == "city")
  22. {
  23. provider.XPath = string.Format("/region/country/province[@name='{0}']/city/@name", name);
  24. }
  25. else if (filter == "town")
  26. {
  27. provider.XPath = string.Format("/region/country/province/city[@name='{0}']/town/@name", name);
  28. }
  29. return provider;
  30. }
  31.  
  32. public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  33. {
  34. throw new NotImplementedException();
  35. }
  36. }

再看以下如何绑定的

  1. <converters:RegionConverter x:Key="region"/>
  1. <ComboBox Grid.Column="" x:Name="country"
  2. DataContext="{Binding Converter={StaticResource region}, ConverterParameter=country}"
  3. SelectedValue="{Binding DataContext.CurrEditorItem.Country,UpdateSourceTrigger=PropertyChanged,
  4. RelativeSource={RelativeSource AncestorType={x:Type mui:ModernWindow}}}"
  5. ItemsSource="{Binding}"
  6. Width="" Style="{StaticResource CommonComboBoxStyle}" />
  7. <ComboBox Grid.Column="" x:Name="province"
  8. DataContext="{Binding SelectedValue, ElementName=country, Converter={StaticResource region}, ConverterParameter=province}"
  9. SelectedValue="{Binding DataContext.CurrEditorItem.Province,UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource AncestorType={x:Type mui:ModernWindow}}}"
  10. ItemsSource="{Binding}"
  11. Width="" Style="{StaticResource CommonComboBoxStyle}" />
  12. <ComboBox Grid.Column="" x:Name="city"
  13. DataContext="{Binding SelectedValue, ElementName=province, Converter={StaticResource region}, ConverterParameter=city}"
  14. SelectedValue="{Binding DataContext.CurrEditorItem.City,UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource AncestorType={x:Type mui:ModernWindow}}}"
  15. ItemsSource="{Binding}"
  16. Width="" Style="{StaticResource CommonComboBoxStyle}" />
  17. <ComboBox Grid.Column="" x:Name="town"
  18. DataContext="{Binding SelectedValue, ElementName=city, Converter={StaticResource region}, ConverterParameter=town}"
  19. SelectedValue="{Binding DataContext.CurrEditorItem.Area,UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource AncestorType={x:Type mui:ModernWindow}}}"
  20. ItemsSource="{Binding}" Text="{Binding Area,UpdateSourceTrigger=PropertyChanged}"
  21. Width="" Style="{StaticResource CommonComboBoxStyle}" />
  22. <TextBlock Grid.Column="" Text="国家" Tag="{Binding SelectedValue, ElementName=country}"
  23. Style="{StaticResource TipTextBlock}"/>
  24. <TextBlock Grid.Column="" Text="省份" Tag="{Binding SelectedValue, ElementName=province}"
  25. Style="{StaticResource TipTextBlock}"/>
  26. <TextBlock Grid.Column="" Text="市/区" Tag="{Binding SelectedValue, ElementName=city}"
  27. Style="{StaticResource TipTextBlock}"/>
  28. <TextBlock Grid.Column="" Text="县/镇" Tag="{Binding SelectedValue, ElementName=town}"
  29. Style="{StaticResource TipTextBlock}"/>

TextBlock放在ComboBox上面,Textblock样式如下

  1. <Style x:Key="TipTextBlock" TargetType="{x:Type TextBlock}">
  2. <Setter Property="IsHitTestVisible" Value="False" />
  3. <Setter Property="HorizontalAlignment" Value="Left"/>
  4. <Setter Property="VerticalAlignment" Value="Center"/>
  5. <Setter Property="Margin" Value="12,0,0,0"/>
  6. <Setter Property="Opacity" Value=""/>
  7. <Style.Triggers>
  8. <Trigger Property="Tag" Value="{x:Null}">
  9. <Setter Property="Opacity" Value=""/>
  10. </Trigger>
  11. </Style.Triggers>
  12. </Style>

枚举在WPF的应用:

  1. <ComboBox x:Name="cbbDataType" ItemsSource="{Binding Source={StaticResource InfoDetailTypeItems}}"
  2. SelectedItem="{Binding CurrEditorItem.DataType, ValidatesOnDataErrors=True}"
  3. ItemTemplate="{StaticResource InfoDetailTypeDataTemplate}"
  4. Grid.Row="" Grid.Column=""
  5. Style="{StaticResource EditorComboBoxStyle}" />
  1. <x:Array x:Key="InfoDetailTypeItems" Type="{x:Type adservice:ShowDataType}">
  2. <adservice:ShowDataType>Image</adservice:ShowDataType>
  3. <adservice:ShowDataType>Video</adservice:ShowDataType>
  4. <adservice:ShowDataType>ThreeDModel</adservice:ShowDataType>
  5. </x:Array>
  1. public ObservableCollection<Employee> userList = new ObservableCollection<Employee>();
  2. public MainWindow()
  3. {
  4. InitializeComponent();
  5. this.List.ItemsSource = userList;
  6.  
  7. ThreadPool.QueueUserWorkItem((m) =>
  8. {
  9. string myfiles = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
  10.  
  11. var query = from pathname in Directory.GetFiles(myfiles) select new { pathtest = pathname };
  12.  
  13. foreach (var item in query)
  14. {
  15. this.Dispatcher.BeginInvoke(new Action(() =>
  16. {
  17. userList.Add(new Employee { Name = item.pathtest });
  18. }));
  19. Thread.Sleep(100);
  20. }
  21.  
  22. });
  23. }
  1. private IList<CategoryTreeDataModel> GenerateTree(IEnumerable<CategoryTreeDataModel> list)
  2. {
  3. IDictionary<int, IList<CategoryTreeDataModel>> childDict = new Dictionary<int, IList<CategoryTreeDataModel>>();
  4.  
  5. // 生成父子关系字典
  6. foreach (var item in list)
  7. {
  8. if (!childDict.ContainsKey(item.ParentId))
  9. childDict.Add(item.ParentId, new List<CategoryTreeDataModel>());
  10. var tempList = childDict[item.ParentId];
  11. tempList.Add(item);
  12. }
  13.  
  14. if (!childDict.ContainsKey(0)) return null;
  15.  
  16. // 生成TreeModel
  17. foreach (var itemList in childDict.Values)
  18. {
  19. foreach (var item in itemList)
  20. {
  21. var parentId = item.CategoryId;
  22. item.Children = childDict.ContainsKey(parentId) ? childDict[parentId] : null;
  23. }
  24. }
  25.  
  26. return GenerateListTree(childDict[0], 0, childDict).ToList();
  27. }
  28.  
  29. /// <summary>
  30. /// 生成拥有子级关系的列表
  31. /// </summary>
  32. /// <param name="list">父级列表</param>
  33. /// <param name="level">树的层级</param>
  34. /// <param name="childDict">父子关系字典</param>
  35. private IEnumerable<CategoryTreeDataModel> GenerateListTree(IEnumerable<CategoryTreeDataModel> list, int level, IDictionary<int, IList<CategoryTreeDataModel>> childDict)
  36. {
  37. if (list == null) yield break;
  38.  
  39. foreach (var item in list)
  40. {
  41.  
  42. yield return item;
  43.  
  44. if (!childDict.ContainsKey(item.CategoryId)) continue;
  45.  
  46. foreach (var childItem in GenerateListTree(childDict[item.CategoryId], level + 1, childDict))
  47. {
  48. yield return childItem;
  49. }
  50. }
  51. }

【WPF】城市级联(XmlDataProvider)的更多相关文章

  1. iOS:城市级联列表的使用

    1.介绍: 现在越来越多的项目都用到了地址,尤其是电商O2O的购物平台,我之前做的教育产品和电商产品都用到了,而实现地址的设置用到的技术就是城市级联列表,即普遍的做法就是自定义选择器控件UIPicke ...

  2. GeneXus笔记本—城市级联下拉

    最近在交流GeneXus的时候 总是会遇到有城市级联下拉的问题 这里就简单做几种方式 供大家参考参考 第一种就是直接绑定关联信息然后在后者的条件模块设定条件即可 具体如下: 首先我们所需要的表为pro ...

  3. WPF:ComboBox使用XmlDataProvider做级联

    程序功能: 使用ComboBox做级联,数据源为XML文件,适合小数据量呈现 程序代码: <Window x:Class="WpfApplication1.LayouTest" ...

  4. WPF:在XmlDataProvider上使用主-从绑定(Master-Detail Binding)

    原文 http://www.cnblogs.com/mgen/archive/2011/06/19/2084553.html 示例程序: 如上程序截图,一目了然典型的主从模式绑定应用,如果里面的数据不 ...

  5. Linq 单表城市级联

    var list = (from province in db.Areas && province.IsDel == join city in db.Areas on province ...

  6. WPF:数据和行为

    如果自己来做一个UI框架,我们会首先关注哪些方面?我想UI框架主要处理的一定包括两个主要层次的内容,一个是数据展现,另一个就是数据操作,所以UI框架必须能够接收各种不同的数据并通过UI界面展现出来,然 ...

  7. WPF的本质:数据和行为

    如果自己来做一个UI框架,我们会首先关注哪些方面?我想UI框架主要处理的一定包括两个主要层次的内容,一个是数据展现,另一个就是数据操作,所以UI框架必须能够接收各种不同的数据并通过UI界面展现出来,然 ...

  8. jquery插件课程1 幻灯片、城市选择、日期时间选择、拖放、方向拖动插件

    jquery插件课程1  幻灯片.城市选择.日期时间选择.拖放.方向拖动插件 一.总结 一句话总结:都是jquery插件,都还比较小,参数(配置参数.数据)一般都是通过json传递. 1.插件配置数据 ...

  9. C#源码500份

    C Sharp  短信发送平台源代码.rar http://1000eb.com/5c6vASP.NET+AJAX基础示例 视频教程 http://1000eb.com/89jcC# Winform ...

随机推荐

  1. 二型错误和功效(Type II Errors and Test Power)

    sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&am ...

  2. vue-router的新奇写法

    加班中........................... 我们以前写路由是下面这样的 这导致了页面一多,我们的路由文件内容就比较多,不好看. 下面我为大家介绍一下,新的一种写法 这种写法,我们只需 ...

  3. hdu4699-Editor

    Sample Input 8 I 2 I -1 I 1 Q 3 L D R Q 2 Sample Output 2 3 发现IDLR四种操作都在光标处发生,且操作完成后光标至多移动1个位置,根据这种“ ...

  4. leetcode 刷题日志 2018-03-26

    58. 最后一个单词的长度 分析:找最后一个非空格,向前找 int lengthOfLastWord(string s) { int i = s.find_last_not_of(' '); int ...

  5. 基础但是很重要的2-sat POJ 3678

    http://poj.org/problem?id=3678 题目大意:就是给你n个点,m条边,每个点都可以取值为0或者1,边上都会有一个符号op(op=xor or and三种)和一个权值c.然后问 ...

  6. POJ3061 Subsequence 尺取or二分

    Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...

  7. oozie与sqoop的简单案例

    1:拷贝模板 2:拷贝hive用的jar包 方式一: 3:编辑job.properties # # Licensed to the Apache Software Foundation (ASF) u ...

  8. 用C++写一个没人用的ECS

    github地址:https://github.com/yangrc1234/Resecs 在做大作业的时候自己实现了一个简单的ECS,起了个名字叫Resecs. 这里提一下一些实现的细节,作为回顾. ...

  9. Xcode下 gdb 调试命令

    Xcode的调试器为用户提供了一个GDB的图形化界面,GDB是GNU组织的开放源代码调试器.您可以在Xcode的图形界面里做任何事情:但是,如果您需要您可以在命令行里使用GDB的命令,且gdb可以在终 ...

  10. Linux实用命令之git-svn

    近日发现了有一个工具,git-svn,可以打通git svn之间的鸿沟. 很适合习惯于git,却需要维护svn代码的同学. 安装 sudo apt-get install git-svn 具体使用就不 ...