• 1000 в SelectedItemBinding. Для ссылки на преобразователь в xaml используется расширение разметки stati c.
<Grid>
<Grid.Resources>
<ObjectDataProvider ObjectType="{x:Type sys:Enum}"
x:Key="asd"
MethodName="GetValues">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="local:enumShortcuts"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Grid.Resources>
<DataGrid ItemsSource="{Binding}"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Keys"
ItemsSource="{Binding Source={StaticResource asd},
Converter={x:Static local:EnumToStringConverters.enum}}"
SelectedItemBinding="{Binding ManagerShortcut,
Converter={x:Static local:StringTOEnumConverter.enum}}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
Здесь преобразователи, используемые для преобразования перечислений туда и обратно.
/// <summary> Converts the enums to string and appends "SHIFTS - " to each item</summary>
public class EnumToStringConverters : IValueConverter
{
public static EnumToStringConverters @enum { get; set; } = new EnumToStringConverters();
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
//Check for null for simplity i didnt inlcude
enumShortcuts[] mies = (enumShortcuts[])value;
return Enum.GetNames(mies[0].GetType()).Select(x => "SHIFT - " + x);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
/// <summary> Converts the string to enum by remove "SHIFTS - " from each item</summary>
public class StringTOEnumConverter : IValueConverter
{
public static StringTOEnumConverter @enum { get; set; } = new StringTOEnumConverter();
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
//Check for null for simplity i didnt inlcude
return "SHIFT - " + value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
//Check for null for simplity i didnt inlcude
return Enum.Parse(typeof(enumShortcuts), value.ToString().Split('-')[1].Replace(" ", ""));
}
}