Databinding RadioButtons to an enum in WPF

Databinding RadioButtons to an enum in WPF

In WPF, you can bind a group of RadioButtons to an enum by using an ItemsControl and a custom DataTemplate. Here's how to do it:

  • Create a new WPF Application:

Create a new WPF Application using Visual Studio or any other C# development environment. Name it EnumRadioButtonBinding.

  • Add an enum:

Add an enum to your project. For this example, we'll create a simple UserRole enum in the MainWindow.xaml.cs file:

public enum UserRole { Administrator, User, Guest } 
  • Add an ObjectDataProvider for the enum:

Open the MainWindow.xaml file and add the following code inside the Window.Resources element to create an ObjectDataProvider for the UserRole enum:

<Window.Resources> <ObjectDataProvider x:Key="EnumDataProvider" MethodName="GetValues" ObjectType="{x:Type sys:Enum}"> <ObjectDataProvider.MethodParameters> <x:Type TypeName="local:UserRole" /> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> </Window.Resources> 

Make sure to add the required namespace references at the top of the MainWindow.xaml file:

xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:EnumRadioButtonBinding" 
  • Create a custom DataTemplate for RadioButtons:

Add a DataTemplate inside the Window.Resources element to define how the RadioButtons will be displayed:

<DataTemplate x:Key="EnumRadioButtonTemplate"> <RadioButton Content="{Binding}" GroupName="UserRole" /> </DataTemplate> 
  • Bind an ItemsControl to the ObjectDataProvider:

Add an ItemsControl to the MainWindow.xaml file and bind its ItemsSource property to the ObjectDataProvider. Set the ItemTemplate property to the custom DataTemplate created in the previous step:

<Grid> <ItemsControl ItemsSource="{Binding Source={StaticResource EnumDataProvider}}" ItemTemplate="{StaticResource EnumRadioButtonTemplate}" /> </Grid> 
  • Run the application:

Now, run the application. You should see a group of RadioButtons displaying the values of the UserRole enum (Administrator, User, and Guest). Users can select only one of the RadioButton options at a time.

This example demonstrates how to bind a group of RadioButtons to an enum in WPF using an ItemsControl and a custom DataTemplate. This approach allows you to bind the RadioButtons directly to the enum values without any additional code in the ViewModel or code-behind.

Examples

  1. Bind Enum to RadioButtons in WPF:

    Bind an enum to a group of RadioButtons in WPF.

    <!-- XAML code --> <StackPanel> <RadioButton Content="Option 1" IsChecked="{Binding Path=SelectedOption, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static local:Options.Option1}}" /> <RadioButton Content="Option 2" IsChecked="{Binding Path=SelectedOption, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static local:Options.Option2}}" /> <RadioButton Content="Option 3" IsChecked="{Binding Path=SelectedOption, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static local:Options.Option3}}" /> </StackPanel> 
    // ViewModel code public enum Options { Option1, Option2, Option3 } private Options _selectedOption; public Options SelectedOption { get { return _selectedOption; } set { if (_selectedOption != value) { _selectedOption = value; OnPropertyChanged(nameof(SelectedOption)); } } } 
  2. RadioButtonFor with Enum in WPF:

    Use RadioButtonFor with an enum in WPF.

    <!-- XAML code --> <StackPanel> <RadioButton Content="Option 1" IsChecked="{Binding Path=SelectedOption, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static local:Options.Option1}}" /> <RadioButton Content="Option 2" IsChecked="{Binding Path=SelectedOption, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static local:Options.Option2}}" /> <RadioButton Content="Option 3" IsChecked="{Binding Path=SelectedOption, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static local:Options.Option3}}" /> </StackPanel> 
    // ViewModel code public enum Options { Option1, Option2, Option3 } private Options _selectedOption; public Options SelectedOption { get { return _selectedOption; } set { if (_selectedOption != value) { _selectedOption = value; OnPropertyChanged(nameof(SelectedOption)); } } } 
  3. Populate RadioButtons from Enum in WPF:

    Populate RadioButtons dynamically from an enum in WPF.

    <!-- XAML code --> <ItemsControl ItemsSource="{Binding Source={x:Type local:Options}}"> <ItemsControl.ItemTemplate> <DataTemplate> <RadioButton Content="{Binding}" IsChecked="{Binding Path=DataContext.SelectedOption, RelativeSource={RelativeSource AncestorType=Window}, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={Binding}}" /> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> 
    // ViewModel code public enum Options { Option1, Option2, Option3 } private Options _selectedOption; public Options SelectedOption { get { return _selectedOption; } set { if (_selectedOption != value) { _selectedOption = value; OnPropertyChanged(nameof(SelectedOption)); } } } 
  4. Create a Group of RadioButtons for Enum in WPF:

    Create a group of RadioButtons for an enum in WPF.

    <!-- XAML code --> <StackPanel> <RadioButton GroupName="OptionsGroup" Content="Option 1" IsChecked="{Binding Path=SelectedOption, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static local:Options.Option1}}" /> <RadioButton GroupName="OptionsGroup" Content="Option 2" IsChecked="{Binding Path=SelectedOption, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static local:Options.Option2}}" /> <RadioButton GroupName="OptionsGroup" Content="Option 3" IsChecked="{Binding Path=SelectedOption, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static local:Options.Option3}}" /> </StackPanel> 
    // ViewModel code public enum Options { Option1, Option2, Option3 } private Options _selectedOption; public Options SelectedOption { get { return _selectedOption; } set { if (_selectedOption != value) { _selectedOption = value; OnPropertyChanged(nameof(SelectedOption)); } } } 
  5. Using Enum as a Data Source for RadioButtons in WPF:

    Use an enum as a data source for RadioButtons in WPF.

    <!-- XAML code --> <ItemsControl ItemsSource="{Binding Source={x:Type local:Options}}"> <ItemsControl.ItemTemplate> <DataTemplate> <RadioButton Content="{Binding}" IsChecked="{Binding Path=DataContext.SelectedOption, RelativeSource={RelativeSource AncestorType=Window}, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={Binding}}" /> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> 
    // ViewModel code public enum Options { Option1, Option2, Option3 } private Options _selectedOption; public Options SelectedOption { get { return _selectedOption; } set { if (_selectedOption != value) { _selectedOption = value; OnPropertyChanged(nameof(SelectedOption)); } } } 
  6. Enum to RadioButton Binding in WPF:

    Perform enum to RadioButton binding in WPF.

    <!-- XAML code --> <StackPanel> <RadioButton Content="Option 1" IsChecked="{Binding Path=SelectedOption, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static local:Options.Option1}}" /> <RadioButton Content="Option 2" IsChecked="{Binding Path=SelectedOption, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static local:Options.Option2}}" /> <RadioButton Content="Option 3" IsChecked="{Binding Path=SelectedOption, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static local:Options.Option3}}" /> </StackPanel> 
    // ViewModel code public enum Options { Option1, Option2, Option3 } private Options _selectedOption; public Options SelectedOption { get { return _selectedOption; } set { if (_selectedOption != value) { _selectedOption = value; OnPropertyChanged(nameof(SelectedOption)); } } } 
  7. WPF RadioButton Enum Converter Example:

    Implement a converter for RadioButton enum binding in WPF.

    <!-- XAML code --> <Window.Resources> <local:EnumToBooleanConverter x:Key="EnumToBooleanConverter" /> </Window.Resources> <StackPanel> <RadioButton Content="Option 1" IsChecked="{Binding Path=SelectedOption, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static local:Options.Option1}}" /> <RadioButton Content="Option 2" IsChecked="{Binding Path=SelectedOption, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static local:Options.Option2}}" /> <RadioButton Content="Option 3" IsChecked="{Binding Path=SelectedOption, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static local:Options.Option3}}" /> </StackPanel> 
    // ViewModel code public enum Options { Option1, Option2, Option3 } private Options _selectedOption; public Options SelectedOption { get { return _selectedOption; } set { if (_selectedOption != value) { _selectedOption = value; OnPropertyChanged(nameof(SelectedOption)); } } } 
  8. RadioButtonList with Enum in WPF:

    Create a RadioButton list with an enum in WPF.

    <!-- XAML code --> <ItemsControl ItemsSource="{Binding Source={x:Type local:Options}}"> <ItemsControl.ItemTemplate> <DataTemplate> <RadioButton Content="{Binding}" IsChecked="{Binding Path=DataContext.SelectedOption, RelativeSource={RelativeSource AncestorType=Window}, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={Binding}}" /> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> 
    // ViewModel code public enum Options { Option1, Option2, Option3 } private Options _selectedOption; public Options SelectedOption { get { return _selectedOption; } set { if (_selectedOption != value) { _selectedOption = value; OnPropertyChanged(nameof(SelectedOption)); } } } 
  9. RadioButton Enum Binding in MVVM WPF:

    Implement RadioButton enum binding in MVVM architecture in a WPF application.

    <!-- XAML code --> <ItemsControl ItemsSource="{Binding Source={x:Type local:Options}}"> <ItemsControl.ItemTemplate> <DataTemplate> <RadioButton Content="{Binding}" IsChecked="{Binding Path=DataContext.SelectedOption, RelativeSource={RelativeSource AncestorType=Window}, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={Binding}}" /> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> 
    // ViewModel code (implementing INotifyPropertyChanged) public enum Options { Option1, Option2, Option3 } private Options _selectedOption; public Options SelectedOption { get { return _selectedOption; } set { if (_selectedOption != value) { _selectedOption = value; OnPropertyChanged(nameof(SelectedOption)); } } } // INotifyPropertyChanged implementation public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } 

More Tags

sapui5 c++ bootstrap-datepicker knockout-2.0 service-accounts deprecation-warning lifecycle distinct uiwebviewdelegate notimplementedexception

More Programming Guides

Other Guides

More Programming Examples