主要思路:

把省的ItemsSource绑定DataContext,然后给市的ItemsSource绑定到Element(省)的SelectedItem上

xaml

 <Window x:Class="Demo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" DataContext="{Binding}">
<Grid>
<TextBlock Height="23" HorizontalAlignment="Left" Margin="27,41,0,0" Name="textBlock1" Text="省" VerticalAlignment="Top" />
<ComboBox Height="23" HorizontalAlignment="Left" Margin="54,36,0,0" Name="cmbProvince" ItemsSource="{Binding}" DisplayMemberPath="Name" VerticalAlignment="Top" Width="120" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="207,36,0,0" Name="textBlock2" Text="市" VerticalAlignment="Top" />
<ComboBox Height="23" HorizontalAlignment="Left" Margin="242,36,0,0" Name="cmbCity" ItemsSource="{Binding SelectedItem.Citys, ElementName=cmbProvince}" DisplayMemberPath="Name" VerticalAlignment="Top" Width="120" />
</Grid>
</Window>

XAML

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace Demo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public List<Province> ProvinceList { set; get; }
public MainWindow()
{
InitializeComponent();
LoadData();
this.cmbProvince.DataContext = ProvinceList;
} /// <summary>
/// 构建数据
/// </summary>
public void LoadData()
{
ProvinceList = new List<Province>();
Province bj = new Province();
bj.Name = "北京"; bj.Citys = new List<City>(); City city1 = new City() { Name = "海淀" };
bj.Citys.Add(city1); city1 = new City() { Name = "朝阳" };
bj.Citys.Add(city1); city1 = new City() { Name = "西城" };
bj.Citys.Add(city1); Province sh = new Province() { Name = "上海" };
sh.Citys = new List<City>();
city1 = new City() { Name = "静安" };
sh.Citys.Add(city1); city1 = new City() { Name = "徐汇" };
sh.Citys.Add(city1); city1 = new City() { Name = "金山" };
sh.Citys.Add(city1); ProvinceList.Add(bj);
ProvinceList.Add(sh);
}
}
}

MainWindow.xaml 的交互逻辑

两个类:

 namespace Demo
{
public class City
{
public string Name { get; set; } }
}

Class City

 using System.Collections.Generic;

 namespace Demo
{
public class Province
{
public string Name { get; set; } public List<City> Citys { get; set; } }
}

Class Province

WPF省市联动Binding的更多相关文章

  1. jquery省市联动,根据公司需求而写

    //author:guan //2015-05-25 //省市联动 //实用说明,页面引用如下js //<script src="../js/jquery-1.6.3.min.js&q ...

  2. Json 基于jQuery+JSON的省市联动效果

    helloweba.com 作者:月光光 时间:2012-09-12 21:57 标签: jQuery  JSON  Ajax  省市联动     省市区联动下拉效果在WEB中应用非常广泛,尤其在一些 ...

  3. AJAX案例四:省市联动

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  4. Dynamic CRM 2013学习笔记(八)过滤查找控件 (类似省市联动)

    我们经常要实现类似省市联动一样的功能,常见的就是二个查找控件,一个选择了省后,另一个市的查找控件就自动过滤了,只显示当前省下的市,而不是所有的市.当然这是最简单的,实际工作中还有更复杂的功能要通过过滤 ...

  5. [JS]以下是JS省市联动菜单代码

    以下是JS省市联动菜单代码: 代码一: <html> <head> <title></title> <script language=" ...

  6. 通过Javascript数组设计一个省市联动菜单

    通过Javascript数组设计一个省市联动菜单 使用Option内置类来完成下拉选项的创建 2.使用定时器实现一个时钟程序 3.使用PHP+JSON完成语音验证码 网址:http://yuyin.b ...

  7. Ado.Net小练习03(省市联动)

    前台界面:          后台代码: namespace _04省市联动 {     public partial class Form1 : Form     {         public ...

  8. Ajax省市联动

    以JQuery为JS,写的Ajax省市联动. 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&q ...

  9. html + ashx 实现Ajax省市联动

    基本思路:1.了解数据库中省和市的表结构及关联主键 2.创建html页面及select标签 3.通过ajax向ashx(一般处理程序)发送后台请求完成联动效果 表结构: 这里,开始创建一个命为demo ...

随机推荐

  1. easyui中tree型控件不正常显示的处理方法

    我在使用easyui中的tree控件时,出现不正常显示的现象,比如li中不能使用自定义的图标.父级展开或关闭时,其子级仍然显现并出现重叠等.找了很多资料,都没解决这个问题,后来逐个对照官方的源码,才找 ...

  2. ASP.NET设置404页面返回302HTTP状态码的解决方法

    在配置文件中配置404页面如下: .代码如下: <customErrors mode="On" defaultRedirect="404.aspx"> ...

  3. CString 转 char*; wstring 转 string

    1. CString  转 char* ); CString name; name.Format(_T("bookUC%d.txt"),m_ID); std::wstring _n ...

  4. iOS上传文件,有关http上传协议-RFC1867

    以上是抓包HTML input file标签上传的内容 只要模拟上面http 的header跟body就可以成功. 整体说明: post 上传文件时,以在http body里面带参数,参数的格式,根据 ...

  5. 利用FlashPaper实现类似百度文库功能

    最近需要实现一个类似百度文库的功能,在Google上淘了一段时间,发现FlashPaper还算能够不错的实现此需求. 首先讲下思路: 1>安装FlashPaper: 2>利用java代码将 ...

  6. C++的优秀特性4:指针

    (转载请注明原创于潘多拉盒子) 其实指针不是C++的特性,而是地地道道的C的特性.有人说C++继承了C的指针,实在是败笔,造成内存泄漏云云,纯粹是不懂.可以这么说,如果没有指针,C++会逊色很多,应用 ...

  7. 一位Erlang程序猿的自白

    12.00 Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE MicrosoftInternetExplorer4 /* Style De ...

  8. os.getcwd()

    1.os.getcwd():获取当前工作目录,也就是在哪个目录下运行这个程序.举例来说:假设F:\aaa\bbb\ccc目录下有个文件 TestDir.py,打印当前工作目录.进入windows命令行 ...

  9. IOS 7 Study - UIDatePicker

    Picking the Date and Time with UIDatePicker effect: 1. declaring a property of type UIDatePicker #im ...

  10. cdoj 1143 传输数据 最大流

    传输数据 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1143 Descri ...