Reputation: 186
I have a DataGridComboBoxColumn which contains a ComboBox of enums (in this instance F1, F2, F3). I want to prefix each item in that ComboBox with a string - in this instance "SHIFT + ", so that the ComboBox displays strings such as "SHIFT + F1", "SHIFT + F2" etc.
Is there a way to do this? I've tried using an IValueConverter and whilst the code executes correctly and breaks on the return value of the converter, the values are not displayed in the ComboBox:
XAML
DataGridComboBoxColumn Header="Keyboard Shortcut"
ItemsSource="{Binding Source={StaticResource enumShortcuts}}"
SelectedItemBinding="{Binding ManagerShortcut, Converter={StaticResource ResourceKey=shortcutConverter}}"/>
Where enumShortcuts are the enums, ManagerShortcut is the property.
Code
if (value != null)
return $"SHIFT + {value}";
Or perhaps there's an even easier way to do this with StringFormat?
Upvotes: 0
Views: 128
Reputation: 662
I assume these the classes to solve the problem
public enum enumShortcuts
{
F1,
F2
}
public class ShortCuts
{
public string Name { get; set; }
public enumShortcuts ManagerShortcut { get; set; }
}
Xaml.cs: In the DataGridComboBoxColumn, we need bind the converters one for converting enum to string to Itemsource property and other for string to enum converters in SelectedItemBinding. For refering converter in the xaml, static markup extension is used.
<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>
Here the converters used for to and fro conversion of enums.
/// <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(" ", ""));
}
}
Upvotes: 1