WPF省市联动Binding_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > WPF省市联动Binding

WPF省市联动Binding

 2014/4/11 2:46:47  佯林  博客园  我要评论(0)
  • 摘要:主要思路:把省的ItemsSource绑定DataContext,然后给市的ItemsSource绑定到Element(省)的SelectedItem上xaml1<Windowx:Class="Demo.MainWindow"2xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"3xmlns:x="http://schemas.microsoft
  • 标签:

主要思路:

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

xaml

class="code_img_closed" src="/Upload/Images/2014041102/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('5a430bc1-e183-402c-ac91-c60cbecd806e',event)" src="/Upload/Images/2014041102/2B1B950FA3DF188F.gif" alt="" />
 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>
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 
15 namespace Demo
16 {
17     /// <summary>
18     /// MainWindow.xaml 的交互逻辑
19     /// </summary>
20     public partial class MainWindow : Window
21     {
22         public List<Province> ProvinceList { set; get; }
23         public MainWindow()
24         {
25             InitializeComponent();
26             LoadData();
27             this.cmbProvince.DataContext = ProvinceList;
28         }
29 
30         /// <summary>
31         /// 构建数据
32         /// </summary>
33         public void LoadData()
34         {
35             ProvinceList = new List<Province>();
36             Province bj = new Province();
37             bj.Name = "北京";
38 
39             bj.Citys = new List<City>();
40 
41             City city1 = new City() { Name = "海淀" };
42             bj.Citys.Add(city1);
43 
44             city1 = new City() { Name = "朝阳" };
45             bj.Citys.Add(city1);
46 
47             city1 = new City() { Name = "西城" };
48             bj.Citys.Add(city1);
49 
50             Province sh = new Province() { Name = "上海" };
51             sh.Citys = new List<City>();
52             city1 = new City() { Name = "静安" };
53             sh.Citys.Add(city1);
54 
55             city1 = new City() { Name = "徐汇" };
56             sh.Citys.Add(city1);
57 
58             city1 = new City() { Name = "金山" };
59             sh.Citys.Add(city1);
60 
61             ProvinceList.Add(bj);
62             ProvinceList.Add(sh);
63         }
64     }
65 }
MainWindow.xaml 的交互逻辑

两个类:

1 namespace Demo
2 {
3     public class City
4     {
5         public string Name { get; set; }
6 
7     }
8 }
Class City
 1 using System.Collections.Generic;
 2 
 3 namespace Demo
 4 {
 5     public class Province
 6     {
 7         public string Name { get; set; }
 8 
 9         public List<City> Citys { get; set; }
10 
11     }
12 }
Class Province

 

  • 相关文章
发表评论
用户名: 匿名