需求:两个ComboBox的级联,实现城市–小区级联。

问题:个人感觉WPF的核心应该是数据绑定这块。由于时间紧迫,粗略看Binding也是一头雾水,所以用了比较简单的方法做了两个下拉列表级联的效果:

在ShellWindow.xaml写上两个ComboBox,命名如下:

    <!-- 城市下拉列表 -->
<ComboBox x:Name="cityComboxBox" Margin="10" Height="20" Width="100"></ComboBox>
<!-- 小区下拉列表 -->
<ComboBox x:Name="communityComboxBox" Height="20" Canvas.Left="80" Canvas.Top="10" Width="177" ></ComboBox>

然后对应的后台代码ShellWindow.xaml.cs如下:

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using WafApplication1.Applications.Views; namespace WafApplication1.Presentation.Views
{
[Export(typeof(IShellView))]
public partial class ShellWindow : Window, IShellView
{
// 准备数据
private Dictionary<string, string[]> cityAndCommunityDictionary = new Dictionary<string, string[]>()
{
{ "南宁", new string[] { "南宁A社区", "南宁B社区", } },
{ "柳州", new string[] { "柳州A社区", "柳州B社区", "柳州C社区", "柳州D社区" } },
{ "桂林", new string[] { "桂林A社区", "桂林B社区", "桂林C社区" } },
}; public ShellWindow()
{
// 初始化控件
InitializeComponent();
// 初始化选择城市的下拉列表
InitCityComboBox();
} /// <summary>
/// 初始化选择城市的下拉列表
/// </summary>
private void InitCityComboBox()
{
// 初始化城市列表
ItemCollection coll = cityComboxBox.Items;
foreach (KeyValuePair<string, string[]> kvp in cityAndCommunityDictionary)
{
ComboBoxItem boxItem = new ComboBoxItem() { Content = kvp.Key };
coll.Add(boxItem);
} // 给ComboBox注册一个选项改变的事件
cityComboxBox.SelectionChanged += new SelectionChangedEventHandler(cityComboxBox_SelectionChanged);
} private void cityComboxBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// 当前城市的社区
ItemCollection coll = communityComboxBox.Items;
// 先清空
coll.Clear();
// 再添加
foreach (KeyValuePair<string, string[]> kvp in cityAndCommunityDictionary)
{
// kvp.Value = { "南宁A社区", "南宁B社区", }
// 此时的 cityComboxBox.SelectedValue = System.Windows.Controls.ComboBoxItem: 南宁
// 所以如果用这种方法获取选中的值,还需要切割字符串
ComboBoxItem selectedCity = cityComboxBox.SelectedItem as ComboBoxItem;
string cityName = selectedCity.Content.ToString(); if (cityName.Equals(kvp.Key))
{
foreach (var item in kvp.Value)
{
// item = "南宁A社区"
ComboBoxItem boxItem = new ComboBoxItem() { Content = item };
coll.Add(boxItem);
}
}
}
}
}
}

运行效果如下:

【WPF】两个下拉列表ComboBox的级联的更多相关文章

  1. 【WPF】获取下拉列表(ComboBox)的选项(ComboBoxItem)的内容

    需求:给下拉列表ComboBox注册一个选项改变时触发的事件,想要获取到当前选中的选项的内容. // 给ComboBox注册一个选项改变的事件 myComboxBox.SelectionChanged ...

  2. 【WPF】给下拉列表ComboBox绑定数据

    思路:给ComboBox控件设置它的ItemSource绑定到ViewModel中的某个列表上,该列表是某个实体类的集合(如List< Person >),而ComboBox列表要显示的是 ...

  3. WPF中DataGrid的ComboBox的简单绑定方式(绝对简单)

    在写次文前先不得不说下网上的其他wpf的DataGrid绑定ComboBox的方式,看了之后真是让人欲仙欲死. 首先告诉你一大堆的模型,一大堆的控件模板,其实或许你紧紧只想知道怎么让combobox怎 ...

  4. [WPF 如何] 如何向 ComboBox 添加一个空白选项

    原文:[WPF 如何] 如何向 ComboBox 添加一个空白选项 看到这个问题,你可能会蔑视一笑 : 这也能成文章? 确实,你只需要在 ItemsSource 的0位置上插入一个空白的项就是了,如: ...

  5. A WPF File ListView and ComboBox

    源码下载: Download FileListView_Version_2.zip Download FileListView_Version_2_Binaries.zip Download File ...

  6. easyUI的combobox实现级联

    先简介下combobox: easyUI重写了select,取而代之的是combobox,有例如以下几种方式能够创建一个combobox 1.使用select标签,并加上class="eas ...

  7. Tkinter的下拉列表Combobox

    Tkinter的下拉列表Combobox   tk中下拉列表使用ttk.Combobox,代码如下:   #!/usr/bin/env python   # -*- coding: utf-8 -*- ...

  8. jquery-easyui combobox combogrid 级联不可编辑实例

    jquery-easyui combobox combogrid 级联不可编辑实例 如何让jquery-easyui的combobox像select那样不可编辑?为combobox添加editable ...

  9. jquery 的combobox 处理级联

    随笔---jquery 的combobox 处理级联 ------------------------html------------- <select id="groupId&quo ...

随机推荐

  1. 深入PHP内核之opcode handler

    1.opcode结构 在Zend/zend_compile.h文件下 struct _zend_op { opcode_handler_t handler; znode_op op1; znode_o ...

  2. iOS升级swift3 遇到Overriding non-open instance method outside of its defining module的解决方案

    最近将我之前的一个swift项目升级swift3,说多了都是泪... 其中,遇到这样一个错误: 这是用的三方:ENSwiftSideMenu时引出的 报了两个错: 1.Cannot inherit f ...

  3. 从零开始的 Python 爬虫速成指南

    序 本文主要内容:以最短的时间写一个最简单的爬虫,可以抓取论坛的帖子标题和帖子内容. 本文受众:没写过爬虫的萌新. 入门 0.准备工作 需要准备的东西: Python.scrapy.一个IDE或者随便 ...

  4. leetcode719:直线上的第k近点对

    问题描述 给定数组a[N],可以确定C(N,2)个点对,也就确定了C(N,2)个距离,求这些距离中第k小的距离(k<C(N,2)). 思路 看到第k小.第k大这种问题,首先想到二分法. 把求值问 ...

  5. 【LeetCode】22. Generate Parentheses (2 solutions)

    Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...

  6. Google官方下拉刷新组件---SwipeRefreshLayout

    今天在Google+上看到了SwipeRefreshLayout这个名词,遂搜索了下,发现竟然是刚刚google更新sdk新增加的一个widget,于是赶紧抢先体验学习下. SwipeRefreshL ...

  7. Ubuntu Pycharm不能同时选中多行解决方法

    转自http://blog.csdn.net/yaoqi_isee/article/details/77866309 问题描述 Pycharm和Sublime有一个很好用的特性就是可以同时选中多行进行 ...

  8. selenium2.0关于python的常用函数

    转: 新建实例driver = webdriver.Chrome() 1.获取当前页面的Url函数 方法:current_url 实例: driver.current_url 2.获取元素坐标 方法: ...

  9. 转 web前端性能分析--原理篇

    转自http://blog.csdn.net/five3/article/details/7686715 web前端性能: 即是web用户在访问一个页面时所要花费的时间总和.即一个完全意义上的用户响应 ...

  10. Report_Report Builder的一些基本概念(概念)

    2014-05-31 Created By BaoXinjian