关于wpf中级联下拉菜单的一些探索

本文探讨了如何在WPF中实现级联下拉菜单,通过创建实体类来表示国、省、市、县,利用绑定机制和INotifyPropertyChanged接口。XAML代码包含样式和模板,而后台代码则展示了简单的数据绑定和值转换器的使用。
摘要由CSDN通过智能技术生成

先上效果图:

(1)建立实体类

由于这个级联菜单的实现用到了绑定机制,所以需要先声明几个实体类,每一个实体类都代表着一个下拉菜单。

 

    public class Country
    {
        private string countryName;
        private List<Province> provinceList;

        public Country(string countryName, List<Province> provinceList)
        {
            this.countryName = countryName;
            this.provinceList = provinceList;
        }
        public Country() { }

        public string CountryName
        {
            get
            {
                return countryName;
            }

            set
            {
                countryName = value;
            }
        }

        internal List<Province> ProvinceList
        {
            get
            {
                return provinceList;
            }

            set
            {
                provinceList = value;
            }
        }
    }


    public class Province
    {
        private string provinceName;

        private List<City> cityList;

        public Province(string provinceName, List<City> cityList)
        {
            this.provinceName = provinceName;
            this.cityList = cityList;
        }
        public Province() { }

        public string ProvinceName
        {
            get
            {
                return provinceName;
            }

            set
            {
                provinceName = value;
            }
        }

        public List<City> CountyList
        {
            get
            {
                return cityList;
            }

            set
            {
                cityList = value;
            }
        }

    }



    public class City
    {
        private string cityName;
        private List<County> countyList;

        public City(string cityName, List<County> countyList)
        {
            this.cityName = cityName;
            this.countyList = countyList;
        }
        public City() { }

        public string CityName
        {
            get
            {
                return cityName;
            }

            set
            {
                cityName = value;
            }
        }

        public List<County> CountyList
        {
            get
            {
                return countyList;
            }

            set
            {
                countyList = value;
            }
        }
    }


    public class County
    {
        private string countyName;

        public County(string countyName)
        {
            this.countyName = countyName;
        }
        public County() { }
        public string CountyName
        {
            get
            {
                return countyName;
            }

            set
            {
                countyName = value;
            }
        }
    }

 

这代表着级联菜单一共有四个:国、省、市、县,然后再加一个作为容器Combobox,当然这也需要声明一个类

    public class UserAddress : INotifyPropertyChanged
    {
        private string userCountry;
        private string userProvince;
        private string userCity;
        private string userCounty;

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            PropertyChanged?.Invoke(this, e);
        }
        public string UserCountry
        {
            get
            {
                return userCountry;
            }

            set
            {
                userCountry = value;
                OnPropertyChanged(new PropertyChangedEventArgs("FinallyAddress"));
            }
        }

        public string UserProvince
        {
            get
            {
                return userProvince;
            }

            set
            {
                userProvince = value;
                OnPropertyChanged(new PropertyChangedEventArgs("FinallyAddress"));
            }
        }

        public string UserCity
        {
            get
            {
                return userCity;
            }

            set
            {
                userCity = value;
                OnPropertyChanged(new PropertyChangedEventArgs("FinallyAddress"));
            }
        }

        public string UserCounty
        {
            get
            {
                return userCounty;
            }

            set
            {
                userCounty = value;
                OnPropertyChanged(new PropertyChangedEventArgs("FinallyAddress"));
            }
        }

        public string FinallyAddress
        {
            get
            {
                return String.Format("{0}:{1}:{2}:{3}",userCountry,userProvince,userCity,userCounty);
            }
        }
    }

 

这个类与上面的那些不同,它继承于INotifyPropertyChanged接口,这也是必要的,这个机制主要是提供了更改通知功能。

 

(2)xaml代码

                        <ComboBox x:Name="addr" Style="{StaticResource SearchComboBoxStyle}" Width="120"  Text="{Binding Path=FinallyAddress,Mode=OneWay}">
                            <ComboBoxItem>
                                <ComboBox x:Name="country" Width="100" Style="{StaticResource InsideComboBoxStyle}" DisplayMemberPath="CountryName"
                                          Text="{Binding Path=UserCountry,Mode=OneWayToSource}"/>
                            </ComboBoxItem>
                            <ComboBoxItem>
                                <ComboBox x:Name="province" Width="100" Style="{StaticResource InsideComboBoxStyle}"  Text="{Binding Path=UserProvince,Mode=OneWayToSource}" 
                                          DisplayMemberPath="ProvinceName" 
                                          ItemsSource="{Binding SelectedItem, ConverterParameter=province, Converter={StaticResource Convert}, ElementName=country}"/>
                            </ComboBoxItem>
                            <ComboBoxItem>
                                <ComboBox x:Name="city" Width="100" Style="{StaticResource InsideComboBoxStyle}" Text="{Binding Path=UserCity,Mode=OneWayToSource}" 
                                          DisplayMemberPath="CityName" 
                                          ItemsSource="{Binding SelectedItem, ConverterParameter=city, Converter={StaticResource Convert}, ElementName=province}"/>
                            </ComboBoxItem>
                            <ComboBoxItem>
                                <ComboBox x:Name="county" Width="100" Style="{StaticResource InsideComboBoxStyle}" Text="{Binding Path=UserCounty,Mode=OneWayToSource}" 
                                          DisplayMemberPath="CountyName" 
                                          ItemsSource="{Binding SelectedItem, ConverterParameter=county, Converter={StaticResource Convert}, ElementName=city}"/>
                            </ComboBoxItem>
                        </ComboBox>

下面的是用到的样式和模板:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:MyQQ.resource.Style">
    <!-- Fill Brushes -->

    <LinearGradientBrush x:Key="NormalBrush" StartPoint="0,0" EndPoint="0,1">
        <Gradi
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值