【WPF】两个下拉列表ComboBox的级联

需求:两个ComboBox的级联,实现城市–小区级联。

问题:个人感觉WPF的核心应该是数据绑定这块。由于时间紧迫,粗略看Binding也是一头雾水,所以用了比较简单的方法做了两个下拉列表级联的效果:

在ShellWindow.xaml写上两个ComboBox,命名如下:

    <!-- 城市下拉列表 -->
    <ComboBox x:Name="cityComboxBox" Margin="10" Height="20" Width="100"></ComboBox>
    <!-- 小区下拉列表 -->
    <ComboBox x:Name="communityComboxBox" Height="20" Canvas.Left="80" Canvas.Top="10" Width="177" ></ComboBox>

然后对应的后台代码ShellWindow.xaml.cs如下:

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using WafApplication1.Applications.Views;

namespace WafApplication1.Presentation.Views
{
    [Export(typeof(IShellView))]
    public partial class ShellWindow : Window, IShellView
    {
        // 准备数据
        private Dictionary<string, string[]> cityAndCommunityDictionary = new Dictionary<string, string[]>()
        {
            { "南宁", new string[] { "南宁A社区", "南宁B社区", } },
            { "柳州", new string[] { "柳州A社区", "柳州B社区", "柳州C社区", "柳州D社区" } },
            { "桂林", new string[] { "桂林A社区", "桂林B社区", "桂林C社区" } },
        };

        public ShellWindow()
        {
            // 初始化控件
            InitializeComponent();
            // 初始化选择城市的下拉列表
            InitCityComboBox();
        }

        /// <summary>
        /// 初始化选择城市的下拉列表
        /// </summary>
        private void InitCityComboBox()
        {
            // 初始化城市列表
            ItemCollection coll = cityComboxBox.Items;
            foreach (KeyValuePair<string, string[]> kvp in cityAndCommunityDictionary)
            {
                ComboBoxItem boxItem = new ComboBoxItem() { Content = kvp.Key };
                coll.Add(boxItem);
            }

            // 给ComboBox注册一个选项改变的事件
            cityComboxBox.SelectionChanged += new SelectionChangedEventHandler(cityComboxBox_SelectionChanged);
        }

        private void cityComboxBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // 当前城市的社区
            ItemCollection coll = communityComboxBox.Items;
            // 先清空
            coll.Clear();
            // 再添加
            foreach (KeyValuePair<string, string[]> kvp in cityAndCommunityDictionary)
            {
                // kvp.Value = { "南宁A社区", "南宁B社区", }
                // 此时的 cityComboxBox.SelectedValue = System.Windows.Controls.ComboBoxItem: 南宁
                // 所以如果用这种方法获取选中的值,还需要切割字符串
                ComboBoxItem selectedCity = cityComboxBox.SelectedItem as ComboBoxItem;
                string cityName = selectedCity.Content.ToString();

                if (cityName.Equals(kvp.Key))
                {
                    foreach (var item in kvp.Value)
                    {
                        // item = "南宁A社区"
                        ComboBoxItem boxItem = new ComboBoxItem() { Content = item };
                        coll.Add(boxItem);
                    }
                }
            }
        }
    }        
}

运行效果如下:
这里写图片描述

原文地址:https://www.cnblogs.com/guxin/p/wpf-tow-combobox-cascade.html