WPF省市联动Binding

主要思路:

把省的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>
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

 

你可能感兴趣的:(bind)