Skip to content

Instantly share code, notes, and snippets.

@5cover
Created June 1, 2023 18:44
Show Gist options
  • Save 5cover/a8c94c71b270efa4dd7e6942dfab45c0 to your computer and use it in GitHub Desktop.
Save 5cover/a8c94c71b270efa4dd7e6942dfab45c0 to your computer and use it in GitHub Desktop.
Converter used to efficiently deal with radio buttons
using System.Globalization;
using System.Windows.Data;
/// <summary>A converter based on equality comparisons between the value and the parameter.</summary>
public sealed class EqualsConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
=> Equals(value, parameter);
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
=> (bool)value
? parameter
: Binding.DoNothing;
}
// Example bindings
// <RadioButton Content="1" IsChecked="{Binding SomeIntProperty, Converter={StaticResource Equals}, ConverterParameter=1}"/>
// <RadioButton Content="2" IsChecked="{Binding SomeIntProperty, Converter={StaticResource Equals}, ConverterParameter=2}"/>
// <RadioButton Content="3" IsChecked="{Binding SomeIntProperty, Converter={StaticResource Equals}, ConverterParameter=3}"/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment