先上效果图:
(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