I want to understand, having an Enum with two values, how to display them on a ListBox with related CheckBoxes, and if they are checked or not
Here is the code:
public enum Type
{
Type_A,
Type_B
}
<ListBox ItemsSource="{Binding Source={Extension:EnumBindingSource {x:Type Model:Type}}}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsChecked}" Content="{Binding Source={Extension:EnumBindingSource {x:Type Model:Type}}}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Here's what I expect
Firsts, declare an EnumHelper class:
namespace YourNamespace
{
public static class EnumHelper
{
public static Array GetValues(Type enumType)
{
return Enum.GetValues(enumType);
}
}
public enum myEnum
{
Type_A,
Type_B,
Type_C,
Type_D,
Type_E
}
}
Then, within your XAML (Let's say MainWindow.xaml), add a reference for your namespace [xmlns:local="clr-namespace:YourNamespace"]
<Window xmlns:local="clr-namespace:YourNamespace">
<!-- Window Content -->
</Window>
And then add this ObjectProvider inside your Windows.Resources
<ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type local:EnumHelper}" x:Key="myEnumKey">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="local:myEnum" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
After which, you can finally use it like this:
<ListBox ItemsSource="{Binding Source={StaticResource myEnumKey}}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>