主要思路:

把省的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. css3妙用 刷新图标

    从猫眼电影看到的一个图标 .geopos i:before { content: ""; width: .8em; height: .8em; border: .14em soli ...

  2. hdoj 5301 Buildings

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5301 #include <iostream> #include <stdio.h&g ...

  3. Odoo 开发者模式

    这里我们以Odoo V9介绍下Odoo的开发者模式: 进入开发者模式: 登录odoo系统后,点击右上角登陆账号下拉菜单,选择About,然后在弹出菜单里点击Activate the developer ...

  4. mongodb的查询方式与sql语句对比

    下面是sql和Mongodb对应的一些语法: SQL Statement Mongo Query Language Statement CREATE TABLE USERS (a Number, b ...

  5. Animation Spinner【项目】

    https://github.com/vjpr/healthkick/blob/master/src/win/healthkick/ucSpinnerCogs.xaml 网上的例子,放在UserCon ...

  6. 重学HTML

    http://www.imooc.com/learn/9 1.em/strong 如果想在一段话中特别强调某几个文字,这时候就可以用到<em>或<strong>标签. 但两者在 ...

  7. Spring 常用工具类

    1) 请求工具类 org.springframework.web.bind.ServletRequestUtils //取请求参数的整数值: public static Integer getIntP ...

  8. JavaScript谁动了你的代码

    到目前为止,同学你知道了JavaScript的历史,也了解其"你想是啥就是啥"的变量系统.相信凭借你深厚的Java或者C++功底,再加上程序员特有的自傲气质,你肯定会信心满满:自信 ...

  9. git免登录-ssh-key

    1.生成ssh key公钥与私钥 ssh-keygen -t rsa -C "youname@example.com" 需输入三个内容:第一个,生成公私钥的路径及名称:后两个输入回 ...

  10. ENTBOOST 2014.180L 发布,开源企业IM免费企业即时通讯

    ENTBOOST,VERSION 2014.180 Linux版本发布,主要增加企业IM应用集成功能,完善安卓SDK功能及部分BUG修正: 7/1(明天)发布Windows版本,敬请关注! ENTBO ...