mirror of
https://github.com/holgerb83/ModernWpfPlayground.git
synced 2025-06-30 09:40:52 +02:00
Reorganization of files
This commit is contained in:
42
Controls/PropertyPresenter/EnumToKeyValueListConverter.cs
Normal file
42
Controls/PropertyPresenter/EnumToKeyValueListConverter.cs
Normal file
@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Windows.Data;
|
||||
|
||||
namespace Controls
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts enums to a List with KeyValuePairs.
|
||||
/// </summary>
|
||||
public class EnumToKeyValueListConverter : IValueConverter
|
||||
{
|
||||
object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
if (!(value is Enum)) return Binding.DoNothing;
|
||||
return (from object enumValue in Enum.GetValues(value.GetType())
|
||||
select new KeyValuePair<string, object>(GetDescription(enumValue), enumValue)).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the content of a description attribute of an enum.
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
private static string GetDescription(object value)
|
||||
{
|
||||
if (!(value is Enum enumValue)) return string.Empty;
|
||||
var descriptionAttribute = enumValue.GetType()
|
||||
.GetField(enumValue.ToString())?.GetCustomAttributes(false)
|
||||
.OfType<DescriptionAttribute>().FirstOrDefault();
|
||||
|
||||
return descriptionAttribute?.Description ?? value.ToString() ?? string.Empty;
|
||||
}
|
||||
|
||||
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
return Binding.DoNothing;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user