I have a List<String> in a custom user control. Whenever the List<String> is modified, I want the custom user control to have checkboxes for every item in the List<String>. My original idea (being used to Java) was to just add the checkboxes and remove them directly.
But… I know C# can do better than that. Is there some way I can “bind” the strings to the UI so they show up as checkboxes? (or any other method that works?)
You can do that easily by binding an
ItemsControlto the list of strings:However that’s not very useful, because you have no easy way to access the state of the
CheckBox… A better option would be to wrap the string in a class that also exposes aboolproperty:You can then expose a list of
CheckableItem<string>instead of a list of strings, and change the XAML as follows:If you need to test if the
CheckBoxis checked, just test theIsCheckedproperty in theCheckableItem<T>class.If you need the
CheckBoxto have 3 states (checked/unchecked/indeterminate), just declare IsChecked asbool?instead ofbool.