mirror of
https://github.com/holgerb83/ModernWpfPlayground.git
synced 2025-06-30 09:40:52 +02:00
features
This commit is contained in:
@ -4,17 +4,20 @@ using System.ComponentModel;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Markup;
|
||||
|
||||
namespace Controls
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts enums to a List with KeyValuePairs.
|
||||
/// </summary>
|
||||
public class EnumToKeyValueListConverter : IValueConverter
|
||||
public class EnumToItemSourceConverter : MarkupExtension, IValueConverter
|
||||
{
|
||||
private static EnumToItemSourceConverter? _converter;
|
||||
|
||||
object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
if (!(value is Enum)) return Binding.DoNothing;
|
||||
if (value is not Enum) return Binding.DoNothing;
|
||||
return (from object enumValue in Enum.GetValues(value.GetType())
|
||||
select new KeyValuePair<string, object>(GetDescription(enumValue), enumValue)).ToList();
|
||||
}
|
||||
@ -40,5 +43,7 @@ namespace Controls
|
||||
{
|
||||
return Binding.DoNothing;
|
||||
}
|
||||
|
||||
public override object ProvideValue(IServiceProvider serviceProvider) => _converter ??= new EnumToItemSourceConverter();
|
||||
}
|
||||
}
|
@ -86,12 +86,11 @@
|
||||
</CheckBox.Style>
|
||||
</CheckBox>
|
||||
</DataTemplate>
|
||||
<controls:EnumToKeyValueListConverter x:Key="EnumToKeyValuePairConverter" />
|
||||
<DataTemplate x:Key="EnumComboBoxDataTemplate">
|
||||
<ComboBox
|
||||
HorizontalAlignment="Stretch"
|
||||
DisplayMemberPath="Key"
|
||||
ItemsSource="{Binding Value, ElementName=LayoutRoot, Converter={StaticResource EnumToKeyValuePairConverter}}"
|
||||
ItemsSource="{Binding Value, ElementName=LayoutRoot, Converter={controls:EnumToItemSourceConverter}}"
|
||||
SelectedValue="{Binding Value, ElementName=LayoutRoot}"
|
||||
SelectedValuePath="Value"
|
||||
Validation.ErrorTemplate="{DynamicResource ValidationErrorTemplate}"
|
||||
|
Reference in New Issue
Block a user