LoginSignup
19
20

More than 5 years have passed since last update.

Enum を ComboBox で表示する方法

Last updated at Posted at 2015-03-06

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>

参考資料

19
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
20