WPF 枚举类型与ComboBox绑定
通过枚举类型建立集合
很多时候,我们需要直接获取枚举值的值,将其绑定到ComboBox之类的控件中。因为我们知道,ComboBox的ItemsSource是一个集合,因此我们需要将枚举值变成一个集合,然后绑定到ComboBox中,可以通过绑定SelectedItem/SelectedValue实现获取选中元素。
枚举定义
public enum AzimuthMapType
{
None=0,
Launch=1,
Receiving=2,
TwoWay=3,
EIRP=4,
}
枚举类型、键值对与ComboBox绑定应用
View Model中通过枚举类型获取键值对集合。
public ControllerViewModel()
{
var source = new Dictionary<int, string>();
foreach (var item in Enum.GetValues(typeof(AzimuthMapType)))
{
source.Add((int)(AzimuthMapType)Enum.Parse(typeof(AzimuthMapType), item.ToString()), item.ToString());
}
AzimuthMapTypeSources=source;
}
public IEnumerable<KeyValuePair<int, string>> AzimuthMapTypeSources
{
get;
private set;
}
public int selectedAmtIndex;
public int SelectedAmtIndex
{
get{return selectedAmtIndex;}
set{
selectedAmtIndex=value;
OnPropertyChanged();
}
}
XAML文件中绑定
<ComboBox Grid.Row="2" Grid.Column="1" Width="150" HorizontalAlignment="Left"
ItemsSource="{Binding AzimuthMapTypeSources}"
DisplayMemberPath="Value"
SelectedValuePath="Key"
SelectedValue="{Binding SelectedAmtIndex, Mode=TwoWay}"/>
枚举类型、普通集合与ComboBox绑定应用
模型定义
public class AzimuthMap : NotifyPropertyChanged
{
public string Name { get { return AzimuthMapType.ToString(); } }
private AzimuthMapType azimuthMapType;
public AzimuthMapType AzimuthMapType
{
get { return azimuthMapType; }
set { azimuthMapType = value; RaisePropertyChanged("Name"); }
}
}
View Model集合获取
public ControllerViewModel()
{
AzimuthMaps = new ObservableCollection<AzimuthMap>();
foreach (var item in Enum.GetValues(typeof(AzimuthMapType)))
{
AzimuthMaps.Add(new AzimuthMap() { AzimuthMapType = (AzimuthMapType)Enum.Parse(typeof(AzimuthMapType), item.ToString()) });
}
private ObservableCollection<AzimuthMap> azimuthMaps;
public ObservableCollection<AzimuthMap> AzimuthMaps
{
get
{
return azimuthMaps;
}
set
{
azimuthMaps = value;
OnPropertyChanged();
}
}
private AzimuthMap selectedAzimuthMap;
public AzimuthMap SelectedAzimuthMap
{
get { return selectedAzimuthMap; }
set { selectedAzimuthMap = value; OnPropertyChanged(); }
}
XAML文件中绑定
<ComboBox Grid.Row="0" Grid.Column="1" DisplayMemberPath="Name"
SelectedItem="{Binding SelectedAzimuthMap,Mode=TwoWay}"
ItemsSource="{Binding AzimuthMaps}"
/>
在XAML中直接绑定枚举值
在XAML中加入枚举类型
<UserControl x:Class="Farsightedgo.Views.ControllerView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Farsightedgo.Views"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:vm="clr-namespace:Farsightedgo.ViewModels"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.DataContext>
<Binding Path="ControllerViewModel" Source="{StaticResource ServiceLocator}"/>
</UserControl.DataContext>
<UserControl.Resources>
<ObjectDataProvider x:Key="AzimuthMapTypes" MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="vm:AzimuthMapType"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</UserControl.Resources>
</UserControl>
在ComboBox中使用
<ComboBox Grid.Row="3" SelectedItem="{Binding SelectedAzimuthMapType ,Mode=TwoWay}" ItemsSource="{Binding Source={StaticResource AzimuthMapTypes}}"/>
在View Model中只需要声明SelectedAzimuthMapType属性即可。
积跬步以至千里:) (:一阵没来由的风