I want to have an Enum on my ViewModel, which in this case represent a set of operations the user can select between. Currently I hard-code each Radio Buttons in the XAML with binding to the respective Enum member, using IValueConverters to get display value and set the selected Enum.
This works fin, however, I would like it to generate and (if possible) group the RadioButtons dynamically for each member in the Enum. Ideally, I would like to have the radio button for an Enum member added and placed in the group (Expander) it belongs, based on the Enum's GroupName.
I have searched and searched for a way to accomplish this, but have as of yet not found anything that fits and is not to complicated for me to understand. But maybe someone out there has some input to help me on the way or an ingenious solution.
Enum:
using System.ComponentModel.DataAnnotations;
namespace CommonLibrary.SystemSetup.Enums
{
public enum UtilityOperation
{
[Display(Name="Reboot", GroupName ="Client Control")]
ClientReboot,
[Display(Name = "Shutdown", GroupName = "Client Control")]
ClientShutdown,
[Display(Name = "Remote Desktop", GroupName = "Client Control")]
ClientRDPControl,
}
}
View:
<UserControl.Resources>
<views:EnumToBoolConverter x:Key="EnumToBoolConverter" />
<views:EnumToDisplayNameConverter x:Key="EnumToNameConverter" />
</UserControl.Resources>
<Expander Foreground="White" VerticalAlignment="Center" Background="Transparent" Margin="10 10 5 5">
<!-- Sub-menu header. -->
<Expander.Header>
<TextBlock Text="Client Control"/>
</Expander.Header>
<StackPanel>
<RadioButton Margin="10 0 0 10"
Content="{Binding Source={x:Static enum:UtilityOperation.ClientReboot}, Converter={StaticResource EnumToNameConverter}}"
IsChecked="{Binding Path=E, Converter={StaticResource EnumToBoolConverter}, ConverterParameter={x:Static enum:UtilityOperation.ClientReboot}}" />
<RadioButton Margin="10 0 0 10"
Content="{Binding Source={x:Static enum:UtilityOperation.ClientShutdown}, Converter={StaticResource EnumToNameConverter}}"
IsChecked="{Binding Path=E, Converter={StaticResource EnumToBoolConverter}, ConverterParameter={x:Static enum:UtilityOperation.ClientShutdown}}" />
<RadioButton Margin="10 0 0 10"
Content="{Binding Source={x:Static enum:UtilityOperation.ClientRDP}, Converter={StaticResource EnumToNameConverter}}"
IsChecked="{Binding Path=E, Converter={StaticResource EnumToBoolConverter}, ConverterParameter={x:Static enum:UtilityOperation.ClientRDP}}" />
</StackPanel>
</Expander>
IValueConverters
public class EnumToBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return parameter != null && parameter.Equals(value);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value != null && value.Equals(true) ? parameter : DependencyProperty.UnsetValue;
}
}
public class EnumToDisplayNameConverter : MarkupExtension, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((Enum)value).GetAttributeOfType<DisplayAttribute>().Name;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
}