I want to create a row of ToggleButtons for Enum values. The buttons must show current value of a property of MyEnumType (by their state) and change the property's value when pressed.
I've found a solution for binding a bunch of separate CheckBoxes to their corresponding enum values (one for each) here, but I'm trying to create the ToggleButtons by ItemsControl (from the Enum type's values) so I wont have to remember to add a ToggleButton every time I add another value to my enum type (and also for shorter XAML code that creates the buttons). The problem is that I can't bind a ConverterParameter. Is there a clean and proper way to do this? Or am I doing everything wrong?
Here is my code now:
<ItemsControl ItemsSource="{Binding Source={local:EnumValues {x:Type local:MyEnumType}}}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ToggleButton Content="{Binding Converter={StaticResource MyEnumToNiceStringConverter}}"
IsChecked="{Binding Source=mySourceObject, Path=SelectedMyEnumValue, Converter={StaticResource EnumBooleanConverter}, ConverterParameter={Binding}}">
</ToggleButton>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
The local:EnumValues
is a MarkupExtension
that returns a list of values from given Enum type.
The EnumBooleanConverter
is a value converter from the above link that returns true if the bound enum value is equal to its ConverterParameter and supports ConvertBack from bool to enum value.
SelectedMyEnumValue
is the property that the buttons are to reflect and modify.
This is a repeating problem for me (that some property cannot be bound) so if you are about to give me a totally different approach for the ToggleButtons, please also write how to work around the problem of such binding. It doesn't have to be bound forever, I just need to set the value once (without a bunch of Style-and-Triggers kind of code in XAML). Maybe another Markup Extension can do this?
Thanks!