WPF – Displaying “Flags” (Enums as Flags)

I started using WPF about a year ago. I liked the way the WPF team managed to split the behaviour of controls from their graphical rendering. This is very powerful and one of the area of ambiguity in the past where logic code ended up in the UI part of your application, making the maintenance more complex and breaking things a lot easier.

During that “learning journey of WPF”, I went through some “challenges” (or issues, depending on how you see the half-full/empty glass of water). This is my first blog to keep track of some of those “challenges” and shared them.

Recently, I had to present to the end user a “list” of choices to filter out items from another control. I used an enumerated type with the “Flags” attribute to define the possible distinct choices.

E.g., in .NET C# you would define a type that represents CRUD permissions (as “flags” or “bits”) as following:

public enum PermissionTypeFlags : int
// Enums to represent CRUDs operations
zeroes = 0,
create = 0x01,
read = 0x02,
update = 0x04,
delete = 0x08,
all = create | read | update | delete

Having such a type defined, how do you go to present it to the end user?

Here are a few thoughts (some might sound crazy, but who knows …):

  • Have a string field (in WPF a <TextBox>) where the user enters the different values separated by a “separator” (e.g. ‘coma’ or ‘space’);
  • A list box where each item in the list represents a flag. The list box allows multiple item selections;
  • A toggle button cycling through all the combination defined by the enumeration type;
  • Multiple check boxes, each one representing a flag.

As for my WPF project, I decided to go with the later, multiple check boxes.

Also, the project was using the MVVM pattern (Model-View-ViewModel). So, the idea is to have a boolean property “Is…{ get; }” defined on the ViewModel for each of the flag. The View can then bind its UI checkboxes to those boolean properties. That way, UI does not know anything about the enumerated type defined by the Model.

Another approach (and with WPF, that is usually the case, there are multiple ways to achieve the same thing) would have been to let the UI know about the enumerated  type “PermissionTypeFlags” and provide a DataConverter (e.g. from “PermissionTypeFlags” to “Boolean”) so that UI controls such as checkboxes can bind their state to the converted value. Here, I decided that UI should not know about the enumerated type.

(Source code available here WpfHelloFlagsVM.zip – rename the file with a “.zip” extension)

Here is an overview of the project :


Previous Post
Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: