본문 바로가기
C# /WPF

[WPF] Enum Type 을 ComboBox에 ItemSource로 바인딩 예제

by Hwoarang757 2024. 9. 16.

(1). ComboBox에 정의할 Enum을 바인딩 합니다.

    public enum FaxTransMethodType
    {
        [Description("즉시 전송")]
        Immediately,
        [Description("예약 전송")]
        Reservation
    }

 

(2). Converter를 작성 합니다.

using GmSFaxAgentWpf.RestApis;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Windows.Data;

namespace GmSFaxAgentWpf.Converters
{
    public class FaxTransValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is FaxTransMethodType faxTransMethodType)
                return GetString(faxTransMethodType);

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is string s)
            {
                if (s == "즉시 전송") s = "Immediately";
                else s = "Reservation";

                return Enum.Parse(typeof(FaxTransMethodType), s);
            }

            return null;
        }

        public string[] Strings => GetStrings();

        public static string GetString(FaxTransMethodType faxTransMethodType)
        {
            return $"{GetDescription(faxTransMethodType)}";
        }

        public static string GetDescription(FaxTransMethodType faxTransMethodType)
        {
            return $"{faxTransMethodType.GetType().GetMember(faxTransMethodType.ToString())[0].GetCustomAttribute<DescriptionAttribute>().Description}";
        }


        public static string[] GetStrings()
        {
            List<string> list = new List<string>();
            foreach(FaxTransMethodType faxTransMethodType in  Enum.GetValues(typeof(FaxTransMethodType)) )
                list.Add(GetString(faxTransMethodType));

            return list.ToArray();
        }
    }
}

 

(3). ResourceDictionary에 등록 합니다.

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


    <!-- 즉시 전송 ,예약전송 정의 진행 -->
    <converters:FaxTransValueConverter x:Key="FaxTransValueConverter" />
</ResourceDictionary>

 

(4). ViewModel에 SelectedItem으로 사용할 항목을 정의 합니다.

        private FaxTransMethodType faxTransMethod = FaxTransMethodType.Immediately;
        public FaxTransMethodType FaxTransMethod
        {
            get => faxTransMethod;
            set
            {
                SetProperty(ref faxTransMethod, value);
            }
        }

 

(5). View 에 Combobox를 정의 합니다.

            <ComboBox VerticalAlignment="Center" 
            HorizontalAlignment="Left"
            Style="{StaticResource ComboBoxStyle}" FontSize="15" Width="200" 
             
             ItemsSource="{Binding Source={StaticResource FaxTransValueConverter},Path=Strings}" 
             SelectedItem="{Binding FaxTransMethod , Converter={StaticResource FaxTransValueConverter}}"
            >
            </ComboBox>

 

실행 결과 입니다.