-
[WPF] ComboBox Enum DescriptionC#/WPF 2025. 6. 19. 12:03EnumToCollectionConverter.cs0.00MB
EnumToCollectionConverter.cs
public static class EnumHelper { public static string Description(this Enum value) { var attributes = value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false); if (attributes.Any()) return (attributes.First() as DescriptionAttribute).Description; // If no description is found, the least we can do is replace underscores with spaces // You can add your own custom default formatting logic here TextInfo ti = CultureInfo.CurrentCulture.TextInfo; return ti.ToTitleCase(ti.ToLower(value.ToString().Replace("_", " "))); } public static IEnumerable<ValueDescription> GetAllValuesAndDescriptions(Type t) { if (!t.IsEnum) throw new ArgumentException($"{nameof(t)} must be an enum type"); return Enum.GetValues(t).Cast<Enum>().Select((e) => new ValueDescription() { Value = e, Description = e.Description() }).ToList(); } } [ValueConversion(typeof(Enum), typeof(IEnumerable<ValueDescription>))] public class EnumToCollectionConverter : MarkupExtension, IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return EnumHelper.GetAllValuesAndDescriptions(value.GetType()); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return null; } public override object ProvideValue(IServiceProvider serviceProvider) { return this; } } public class ValueDescription { public object Value { get; set; } public object Description { get; set; } }Model.cs || ViewModel.cs

xaml


https://stackoverflow.com/questions/6145888/how-to-bind-an-enum-to-a-combobox-control-in-wpf
https://dlsenfl.tistory.com/entry/C-Enum-enum
[C#] Enum, enum
https://learn.microsoft.com/ko-kr/dotnet/api/system.enum.getnames?view=net-9.0 Enum.GetNames 메서드 (System)지정된 열거형에서 상수 이름의 배열을 검색합니다.learn.microsoft.com Enum value 이름들 가져오기Enum.GetNames(typeof(In
dlsenfl.tistory.com
728x90'C# > WPF' 카테고리의 다른 글
[WPF] Custom Control (0) 2025.08.20 [WPF] TargetName (1) 2025.07.09 [WPF] Constructor, Property Create order(생성자 프로퍼티 생성순서) (0) 2025.06.17 [WPF][별***] Net6+ Com host Object ( 32/64bit ) (0) 2025.06.12 [WPF] WpfAppMcpClient Sample (0) 2025.06.12 댓글