I would like to bind a checkbox list to a collection of enum values in WPF.
The enum is not [Flags].
Context:
It is for filtering a datagrid, in which each item has a instance of my enum.
It doesn’t necessarily need to bind to an List, a fixed size collection of would work as well.
Assuming you want to bind to all possible values of your enum, you can do it with an ObjectDataProvider.
Declare this in your resources (
Window.ResourcesorApp.Resourcesetc.):This basically represents a call to
Enum.GetValues(typeof(TestEnum))and exposes it as a data source.Note: You need to declare the namespaces
sysandlocalbefore, wheresysisclr-namespace:System;assembly=mscorlibandlocalis the namespace of your enum.Once you have that, you can use that ObjectDataProvider as a Binding source just like anything else, for example:
The non-declarative way of doing this is just assigning that in code:
For binding the selected items, unfortunately the SelectedItems property cannot be set from Xaml, but you can use the SelectionChanged event:
and then set the property on your ViewModel (or whatever you use) in the event: