【WPF】两个下拉列表ComboBox的级联
需求:两个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的级联的更多相关文章
- 【WPF】获取下拉列表(ComboBox)的选项(ComboBoxItem)的内容
需求:给下拉列表ComboBox注册一个选项改变时触发的事件,想要获取到当前选中的选项的内容. // 给ComboBox注册一个选项改变的事件 myComboxBox.SelectionChanged ...
- 【WPF】给下拉列表ComboBox绑定数据
思路:给ComboBox控件设置它的ItemSource绑定到ViewModel中的某个列表上,该列表是某个实体类的集合(如List< Person >),而ComboBox列表要显示的是 ...
- WPF中DataGrid的ComboBox的简单绑定方式(绝对简单)
在写次文前先不得不说下网上的其他wpf的DataGrid绑定ComboBox的方式,看了之后真是让人欲仙欲死. 首先告诉你一大堆的模型,一大堆的控件模板,其实或许你紧紧只想知道怎么让combobox怎 ...
- [WPF 如何] 如何向 ComboBox 添加一个空白选项
原文:[WPF 如何] 如何向 ComboBox 添加一个空白选项 看到这个问题,你可能会蔑视一笑 : 这也能成文章? 确实,你只需要在 ItemsSource 的0位置上插入一个空白的项就是了,如: ...
- A WPF File ListView and ComboBox
源码下载: Download FileListView_Version_2.zip Download FileListView_Version_2_Binaries.zip Download File ...
- easyUI的combobox实现级联
先简介下combobox: easyUI重写了select,取而代之的是combobox,有例如以下几种方式能够创建一个combobox 1.使用select标签,并加上class="eas ...
- Tkinter的下拉列表Combobox
Tkinter的下拉列表Combobox tk中下拉列表使用ttk.Combobox,代码如下: #!/usr/bin/env python # -*- coding: utf-8 -*- ...
- jquery-easyui combobox combogrid 级联不可编辑实例
jquery-easyui combobox combogrid 级联不可编辑实例 如何让jquery-easyui的combobox像select那样不可编辑?为combobox添加editable ...
- jquery 的combobox 处理级联
随笔---jquery 的combobox 处理级联 ------------------------html------------- <select id="groupId&quo ...
随机推荐
- HighCharts终极版本
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- maven 亲测可用国内镜像 阿里云(20170123)
来源于:http://www.jianshu.com/p/4d5bb95b56c5 http://blog.csdn.net/qq_27093465/article/details/52982484 ...
- 深入PHP内核之函数和返回值
1.关于返回值,PHP内核中使用了大量的宏来实现,我们先看一个函数 PHP_FUNCTION 宏的定义(Zend/zend_API.h) #define PHP_FUNCTION ZEND_FUNC ...
- PHP-Ajax跨域解决方案
1.先了解下Ajax跨域问题: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "ht ...
- windows批处理文件打印幻方
无论是批处理文件还是shell都是没有意义的,它们只是一种工具,并且是非常低级难懂的工具. 如果不会,那就保持不会就好了.不要花费太多时间在这些没意义的事情上. 批处理的没意义体现在: 难以表达 随便 ...
- linux的cat命令
1 描述 cat 的全称 concatenate files and print on the standard output cat命令事Linux下的一个文本输出命令. 用于链接文件并打印到标准输 ...
- MySQL "replace into" 的坑以及insert相关操作
下面我们主要说一下在插入时候的几种情况: 1:insert ignore 2:replace into 3:ON DUPLICATE KEY UPDATE 关于insert ignore: 关于rep ...
- JDK1.5新特性,语言篇
Java 1.5版本,就是Java 2 Standard Edition 5,Version 1.5,简称Java 5.版本代号Tiger. 一. 泛型(Generics) C++通过模板技术可以指定 ...
- A-Frame WebVR开发新手教程
WebVR和WebGL应用程序接口使得我们已经能够在浏览器上创建虚拟现实(VR)体验.但从project化的角度而言,开发社区还须要很多其它方便强大的开发库来简化编程.Mozilla的 A-Frame ...
- 修改npm下载模块的安装位置
默认安装完node.js后会自己安装npm,通过npm下载全局模块默认安装到C:\Users\user\AppData\Roaming目录下,主要有两个文件夹:npm.npm-cache npm:下载 ...