Skip to content

Instantly share code, notes, and snippets.

@mike-kilo
Last active November 23, 2025 12:18
Show Gist options
  • Select an option

  • Save mike-kilo/412a3bd3fc9d31205b4745fa61374376 to your computer and use it in GitHub Desktop.

Select an option

Save mike-kilo/412a3bd3fc9d31205b4745fa61374376 to your computer and use it in GitHub Desktop.
AvaloniaUI RadioButtons Enum bind
using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
namespace Helpers;
public partial class EnumedRadios<T> : ObservableObject where T : Enum
{
public event EventHandler<EventArgs>? SelectedValueChanged;
public IEnumerable<T> Values { get; private set; } = Enum.GetValues(typeof(T)).Cast<T>();
[ObservableProperty]
[AllowNull]
private T _selectedValue;
partial void OnSelectedValueChanged(T value) => SelectedValueChanged?.Invoke(this, new EventArgs());
}
https://mike.kozlowski.nl/doku.php?id=radiobuttons_enum_bind
<ListBox
ItemsSource="{Binding <<MyVariable>>.Values}"
SelectedValue="{Binding <<MyVariable>>.SelectedValue, Mode=TwoWay}">
<ListBox.Styles>
<Style Selector="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<RadioButton
Content="{TemplateBinding ContentPresenter.Content}"
IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.Styles>
</ListBox>
public enum Options
{
Zeroth = 0,
First,
Second,
Third,
}
[ObservableProperty]
private EnumedRadios<Options> _myOptions = new();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment