WPF 枚舉類型與ComboBox綁定

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屬性即可。


積跬步以至千里:) (:一陣沒來由的風

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章