A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© haxyek 金牌黑马   /  2014-4-11 00:03  /  2419 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 haxyek 于 2014-4-11 00:06 编辑

主要思路:
把省的ItemsSource绑定DataContext,然后给市的ItemsSource绑定到Element(省)的SelectedItem上
xaml
  1. <Window x:Class="Demo.MainWindow"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         Title="MainWindow" Height="350" Width="525" DataContext="{Binding}">
  5.     <Grid>
  6.         <TextBlock Height="23" HorizontalAlignment="Left" Margin="27,41,0,0" Name="textBlock1" Text="省" VerticalAlignment="Top" />
  7.         <ComboBox Height="23" HorizontalAlignment="Left" Margin="54,36,0,0" Name="cmbProvince" ItemsSource="{Binding}" DisplayMemberPath="Name" VerticalAlignment="Top" Width="120" />
  8.         <TextBlock Height="23" HorizontalAlignment="Left" Margin="207,36,0,0" Name="textBlock2" Text="市" VerticalAlignment="Top" />
  9.         <ComboBox Height="23" HorizontalAlignment="Left" Margin="242,36,0,0" Name="cmbCity" ItemsSource="{Binding SelectedItem.Citys, ElementName=cmbProvince}"  DisplayMemberPath="Name" VerticalAlignment="Top" Width="120" />
  10.     </Grid>
  11. </Window>
复制代码
C#
MainWindow.xaml 的交互逻辑
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;

  14. namespace Demo
  15. {
  16.     /// <summary>
  17.     /// MainWindow.xaml 的交互逻辑
  18.     /// </summary>
  19.     public partial class MainWindow : Window
  20.     {
  21.         public List<Province> ProvinceList { set; get; }
  22.         public MainWindow()
  23.         {
  24.             InitializeComponent();
  25.             LoadData();
  26.             this.cmbProvince.DataContext = ProvinceList;
  27.         }

  28.         /// <summary>
  29.         /// 构建数据
  30.         /// </summary>
  31.         public void LoadData()
  32.         {
  33.             ProvinceList = new List<Province>();
  34.             Province bj = new Province();
  35.             bj.Name = "北京";

  36.             bj.Citys = new List<City>();

  37.             City city1 = new City() { Name = "海淀" };
  38.             bj.Citys.Add(city1);

  39.             city1 = new City() { Name = "朝阳" };
  40.             bj.Citys.Add(city1);

  41.             city1 = new City() { Name = "西城" };
  42.             bj.Citys.Add(city1);

  43.             Province sh = new Province() { Name = "上海" };
  44.             sh.Citys = new List<City>();
  45.             city1 = new City() { Name = "静安" };
  46.             sh.Citys.Add(city1);

  47.             city1 = new City() { Name = "徐汇" };
  48.             sh.Citys.Add(city1);

  49.             city1 = new City() { Name = "金山" };
  50.             sh.Citys.Add(city1);

  51.             ProvinceList.Add(bj);
  52.             ProvinceList.Add(sh);
  53.         }
  54.     }
  55. }
复制代码
Class City
  1. namespace Demo
  2. {
  3.     public class City
  4.     {
  5.         public string Name { get; set; }

  6.     }
  7. }
复制代码
Class Province
  1. using System.Collections.Generic;

  2. namespace Demo
  3. {
  4.     public class Province
  5.     {
  6.         public string Name { get; set; }

  7.         public List<City> Citys { get; set; }

  8.     }
  9. }
复制代码

http://www.cnblogs.com/9527y/p/3657732.html




评分

参与人数 1技术分 +1 收起 理由
czwanglei + 1

查看全部评分

1 个回复

倒序浏览
{:3_65:}虽然看不懂,但抢个沙发还是可以的,且坐且珍惜
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马