Last active
November 23, 2025 12:18
-
-
Save mike-kilo/412a3bd3fc9d31205b4745fa61374376 to your computer and use it in GitHub Desktop.
AvaloniaUI RadioButtons Enum bind
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| https://mike.kozlowski.nl/doku.php?id=radiobuttons_enum_bind |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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