I have an enumeration type for user privileges that looks like this:
[Flags] public enum UserPrivileges : byte { None = 0, // 0000 0000 View = 1 << 0, // 0000 0001 Import = 1 << 1, // 0000 0010 Export = 1 << 2, // 0000 0100 Supervisor = View | Import | Export | 1 << 3, // 0000 1111 Admin = Supervisor | 1 << 4 // 0001 1111 }
These values ββare bound to CheckBoxes in a graphical interface with a value converter. (I wanted to make this as general as possible, because there are other privileges (like EmployeePrivileges))
public class ByteFlagsEnumValueConverter : IValueConverter { private byte _targetValue; public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var mask = (byte)parameter; _targetValue = (byte)value; return ((mask | _targetValue) == _targetValue); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { var mask = (byte)parameter; if ((bool)value) { _targetValue |= mask; } else {
This works great for displaying and adding privileges to a user in a graphical interface. It also works to remove Superflags as a Supervisor (all flags >= Supervisor are removed, other flags do not change).
The problem is that I will clear the "Import" checkbox, for example, I want to remove all Superflags (Supervisor, Admin), but would like to save other flags (View, Export).
0001 1111 // Admin 0000 0010 // Import --------- 0000 0101 // View | Export
But I didnβt figure out how to do it. Anyboy who has a good solution for this?
source share