views:

9

answers:

1

I bind a TextBox`s Brush Property to an IsValid Dependency Property in a UserControl with a BoolToBrushConverter. My IsValid would need 4 states because I need 4 different brushes to return from the converter. Is there another way using strings? instead of bool, could that work?

+1  A: 

Sure. You can convert whatever you want to whatever you want. You just need to implement the way how it is converted.

However, if the number of states is limited to 4, I would suggest using an Enum instead of strings because this makes it safer regarding refactoring etc.

Something like that should work:

internal enum State
{
    State1, State2, State3, State4
}

// ...

public void Convert(object value, ...)
{
    if (value is State)
    {
        State state = (State)value;
        switch(state)
        {
            case State.State1:
                return myBrush1;
            case State.State2:
                return myBrush2;
            case State.State3:
                return myBrush3;
            case State.State4:
                return myBrush4;
        }
    }

    return defaultBrush;
}

BTW: Depending on the scenario, it might be better to use triggers, but that is not always possible.

gehho