Title: Bind CheckBox to enum value
Tags: c#;wpf
Question: This seems like a very basic idea I just haven't been able to find an answer anywhere. I want to bind an enum type or class to a checkbox similar to how it can be done with a ComboBox. For example:
public class MyClass
public int Value {get; set;}
public string Display {get; set;}
Then in the XAML bind to the class like:
<ComboBox x:Name=&quot;comboBoxMyCb&quot; ItemsSource=&quot;{Binding ListOfMyClass}&quot; DisplayMemberPath=&quot;Display&quot; SelectedValuePath=&quot;Value&quot;/&gt;
Then when an event happens I can get the value later like so:
int val = (int)comboBoxMyCb.SelectedValue;
Basically I want to do a similar thing but simpler. I am making a list of enum values and each value corresponds to a checkbox. My current set up is when I close the windows I check which checkboxes have been checked and add their corresponding enum value to the list. This makes for a lot of if statements because their can be upwards of 40 checkboxes. What I would like to do is bind each checkbox to an enum value in the XAML then set each of their onclick events to a function that looks something like this:
private void CheckBoxItem_Click(object sender, RoutedEventArgs e)
CheckBox cb = (CheckBox)sender;
MyEnum value = (MyEnum)cb.value;//or whatever
if (cb.IsChecked == true)
EnumList.add(value)
else
EnumList.remove(value);
I know some of the code is not optimal I just wrote this all as an example.
Thank you for any help

Here is the accepted answer: If you want to create each CheckBox in XAML, you can assign the enum type to the Tag property.
Gets or sets an arbitrary object value that can be used to store custom information about this element.
I created an enum MyEnum with constants MyEnumConstant1 and so on, replace it with your own.
<CheckBox Content=&quot;My enum constant 1&quot; Tag=&quot;{x:Static local:MyEnum.MyEnumConstant1}&quot; Checked=&quot;EnumCheckBoxChecked&quot;/&gt;
<CheckBox Content=&quot;My enum constant 2&quot; Tag=&quot;{x:Static local:MyEnum.MyEnumConstant2}&quot; Checked=&quot;EnumCheckBoxChecked&quot;/&gt;
<!-- ...the other 38 check boxes. --&gt;
Using the Checked event, you can get the Tag and add the enum value to EnumList or remove it. Since I do not know exactly what your EnumList is, I leave it like it is in this sample code.
private void EnumCheckBoxChecked(object sender, RoutedEventArgs e)
var checkBox = (CheckBox)sender;
var value= checkBox.Tag;
if (checkBox.IsChecked.GetValueOrDefault())
EnumList.add(value);
else
EnumList.remove(value);
The IsChecked property is a nullable bool. GetValueOrDefault will return false if it is null.
You can also do this in an MVVM compliant way by using view models and binding to a command.
<CheckBox Content=&quot;My enum value 1&quot; Command=&quot;{Binding CheckedCommand}&quot; CommandParameter=&quot;{x:Static local:MyEnum.MyEnumValue1}&quot;/&gt;
Here, you do not need the Tag property, as you pass the enum constant as command parameter.

Here is another answer: You could write a wrapper class to represent your Enum. For example, consider the sample enum
public enum SampleEnum
[Description(&quot;Description 01&quot;)]
Sample01,
[Description(&quot;Description 02&quot;)]
Sample02,
[Description(&quot;Description 03&quot;)]
Sample03
Now you could write a wrapper as
public class Wrapper
public SampleEnum Value { get; set; }
public string Description =&gt; GetEnumDescription(Value);
public bool IsSelected { get; set; }
private string GetEnumDescription(Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes.Length &gt; 0)
return attributes[0].Description;
else
return value.ToString();
}
You could now create a collection in your view model as
public List<Wrapper&gt; EnumCollection { get; set; }
Which could be initialized as
EnumCollection = Enum.GetValues(typeof(SampleEnum))
.Cast<SampleEnum&gt;()
.Select(x =&gt; new Wrapper { Value = x, }).ToList();
This Collection can now be bound to your collection of Checkbox. For example,
<ItemsControl ItemsSource=&quot;{Binding EnumCollection}&quot;&gt;
<ItemsControl.ItemTemplate&gt;
<DataTemplate&gt;
<CheckBox Content=&quot;{Binding Description}&quot; IsChecked=&quot;{Binding IsSelected}&quot;/&gt;
</DataTemplate&gt;
</ItemsControl.ItemTemplate&gt;
</ItemsControl&gt;
The IsSelected flag would help you trace if the particular enum is selected.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:11/02/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><wpf>', 'Title': 'Bind CheckBox to enum value', 'ViewCount': 723, 'id': 64654260, 'user_AboutMe': '', 'user_AccountId': 5217081.0, 'user_DisplayName': 'Mavese', 'user_Reputation': 131.0, 'user_Views': 36.0, 'user_id': 4171690.0}}