[Silverlight][C#] 在Silverlight中取得列舉型別中的所有項目並Binding到下拉式選單

  • 9879
  • 0
  • C#
  • 2013-07-14

在C#中可以很簡單的透過Enum.GetValues()或是Enum.GetNames()來取得列舉型別中項目的值或是名稱,但是在Silvelright中想要使用同一招的話就會發現,咦!?怎麼行不通了!?Silverlight中的Enum完全不提供Enum.GetValues()和Enum.GetNames()這兩個Method啊!!!~~~

 

在C#中可以很簡單的透過Enum.GetValues()或是Enum.GetNames()來取得列舉型別中項目的值或是名稱,但是在Silvelright中想要使用同一招的話就會發現,咦!?怎麼行不通了!?Silverlight中的Enum完全不提供Enum.GetValues()和Enum.GetNames()這兩個Method啊!!!~~~

自然在[C#]使用LINQ取出列舉中的所有項目一文中使用的方法在Silverlight中就完全的不管用了,一整個殘念....

 

不過,我們還是可以透過其他的方式來達到這個目的,就是借重超強大的Reflection來取得列舉型別中的LiteralField,所以這次我們一樣要建立一個EnumHelper,不過內容改為如下:

EnumHelper.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
 
public class EnumHelper
{
    public static List<T> ToList<T>()
    {
        Type enumType = typeof( T );
 
        if( !enumType.IsEnum )
        {
            throw new ArgumentException( "Type '" + enumType.Name + "' is not an enum" );
        }
 
        List<T> values = new List<T>();
 
        var fields = from field in enumType.GetFields()
                     where field.IsLiteral
                     select field;
 
        foreach( FieldInfo field in fields )
        {
            object value = field.GetValue( enumType );
            values.Add( ( T ) value );
        }
 
        return values;
    }
 
}

 

來個簡單的小範例吧!!

MainPage.xaml

<UserControl x:Class="SL_BindingEnums.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="600" d:DesignWidth="800">
 
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        
        <StackPanel VerticalAlignment="Center" Margin="10" >
            <TextBlock Text="Visibility:"/>
            <ComboBox Name="cmbVisibility"/>
            <TextBlock Text="DayOfWeek:"/>
            <ComboBox Name="cmbDayOfWeek"/>
        </StackPanel>
 
        <Canvas Grid.Column="1"  VerticalAlignment="Center" HorizontalAlignment="Center" Width="100" Height="100" Background="R"
                 Visibility="{Binding ElementName=cmbVisibility, Path=SelectedItem}" />
        
    </Grid>
</UserControl>

 

MainPage.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
 
namespace SL_BindingEnums
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
 
            this.cmbVisibility.ItemsSource = EnumHelper.ToList<Visibility>();
 
            this.cmbDayOfWeek.ItemsSource = EnumHelper.ToList<DayOfWeek>();
        }
    }
}

 

將列舉型別裡面的項目轉成List之後要Binding就是這麼的簡單!!

 

範例執行的結果如下:

老樣子,最後附上專案原始檔,請自行服用: