Enum を ComboBox で表示する方法

  • 10
    いいね
  • 0
    コメント
この記事は最終更新日から1年以上が経過しています。

C# の WPF で enum の値を ComboBox に入れる方法。よく忘れるのでメモ。

Enum のリソース定義

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:system="clr-namespace:System;assembly=mscorlib">

    <ObjectDataProvider x:Key="DayOfWeeks" MethodName="GetValues" ObjectType="{x:Type system:DayOfWeek}">
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="system:DayOfWeek"/>
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>

</ResourceDictionary>

Converter の準備

namespace MyApp.Views.Converters
{
    using System;
    using System.Windows.Data;


    [ValueConversion(typeof(DayOfWeek), typeof(string))]
    public class DayOfWeekConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            // DayOfWeek => string
            if (value != null && value is DayOfWeek)
            {
                switch ((DayOfWeek)value)
                {
                    case DayOfWeek.Sunday:
                        return "日曜日";

                    case DayOfWeek.Monday:
                        return "月曜日";

                    case DayOfWeek.Tuesday:
                        return "火曜日";

                    case DayOfWeek.Wednesday:
                        return "水曜日";

                    case DayOfWeek.Thursday:
                        return "木曜日";

                    case DayOfWeek.Friday:
                        return "金曜日";

                    case DayOfWeek.Saturday:
                        return "土曜日";

                    default:
                        break;
                }
            }

            return DependencyProperty.UnsetValue;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            // string => DayOfWeek
            if (value != null && value is string)
            {
                switch ((string)value)
                {
                    case "日曜日":
                        return DayOfWeek.Sunday;

                    case "月曜日":
                        return DayOfWeek.Monday;

                    case "火曜日":
                        return DayOfWeek.Tuesday;

                    case "水曜日":
                        return DayOfWeek.Wednesday;

                    case "木曜日":
                        return DayOfWeek.Thursday;

                    case "金曜日":
                        return DayOfWeek.Friday;

                    case "土曜日":
                        return DayOfWeek.Saturday;

                    default:
                        break;
                }
            }

            return DependencyProperty.UnsetValue;
        }
    }
}

Converter のリソース登録

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:converters="clr-namespace:MyApp.Views.Converters">

    <converters:DayOfWeekConverter x:Key="DayOfWeekConverter"/>

</ResourceDictionary>

View

<ComboBox ItemsSource="{Binding Source={StaticResource DayOfWeeks}}"
          SelectedItem="{Binding SelectedDayOfWeek}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Converter={StaticResource DayOfWeekConverter}}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

参考資料