acg
acg

Reputation: 47

Bind IsChecked to bitfield

I have a bitfield like this:

[Flags]
public enum EmployeeType
{
    None = 0,
    IsInOffice = 1,
    IsManager = 2,
    IsOnVacation = 4,
    ...
}

My UI has a listbox of all employees and I want to implement checkboxes to filter the list. This listbox is bound to a filter property like this:

public IEnumerable<Employee> FilteredEmployees
    => _employees.Where(e => _regexFilter.IsMatch(e.FullName)
                                && (!EmployeeType.HasFlag(EmployeeType.IsInOffice) || e.IsLoggedIn)
                                && (!EmployeeType.HasFlag(EmployeeType.IsManager) || e.RoleID >= 4)
                                && (!EmployeeType.HasFlag(EmployeeType.IsOnVacation) || TimeRec.GetStatusID(e.ID) == 7))
                 .OrderBy(e => e.FullName);

My problem now is that I can't get a correct return value from the converter, since the current state of all flags is not known there and I also can't manage to get the current state of all flags into the converter.

I'm able to activate a flag but I'm unable to deactivate any flag without deactivating or activating others, which would be unintended behavior.

Upvotes: 1

Views: 40

Answers (1)

acg
acg

Reputation: 47

Seems like I got it using this:

Converter:

if ((bool)value)
    return (EmployeeType)parameter;
else
    return ~(EmployeeType)parameter;

Setter:

if ((int)value > 0)
    _employeeType |= value;
else
    _employeeType &= value;

Not 100% sure if it's a hack that just works in my scenario but well, it works :D

Upvotes: 1

Related Questions