title | author | description | keywords |
---|---|---|---|
EnumValuesExtensions |
Sergio0694 |
A markup extension that returns a collection of values of a specific enum type. |
windows 10, uwp, uwp community toolkit, uwp toolkit, markup extension, XAML, markup, enum |
The EnumValuesExtensions
type implements a markup extension that returns a collection of values of a specific enum type. It can be useful to easily bind a collection of all possible values from a given enum type to a UI element such as a ComboBox
or some other items container or selector control.
Platform APIs:
EnumValuesExtensions
Assuming we had an Animal
enum type and we wanted the user to pick one of the available names, here is the XAML syntax that allows us to create a ComboBox
and display all Animal
values, directly from XAML and with no code-behind:
<ComboBox
xmlns:ui="using:Microsoft.Toolkit.Uwp.UI"
xmlns:enums="using:MyApplication.Enums"
ItemsSource="{ui:EnumValues Type=enums:Animal}"
SelectedIndex="0"/>
In this example we're just relying on the default ComboBox
item template, that will display the name of each Animal
value in a TextBlock
control. We could of course also define a custom item template if we wanted to show additional info for each individual Animal
value, or if we wanted to further customize how each value is presented to the user.
Binding to an enum property can be accomplished like so:
<ComboBox
xmlns:ui="using:Microsoft.Toolkit.Uwp.UI"
xmlns:enums="using:MyApplication.Enums"
ItemsSource="{ui:EnumValues Type=enums:Animal}"
SelectedItem="{x:Bind SelectedAnimal, Mode=OneWay}" />
private Animal selectedAnimal = Animal.Dog;
public Animal SelectedAnimal
{
get => selectedAnimal;
set
{
selectedAnimal = value;
OnPropertyChanged(nameof(SelectedAnimal));
}
}
You can find more examples in the unit tests.