bas
bas

Reputation: 14912

Can't seem to get my IValueConverter to work

Have the following simple xaml

<UserControl.Resources>
        <converters:StateToColorConverter x:Key="stateToColorConverter"/>
</UserControl.Resources>
<StackPanel>
    <Grid Width="150" Height="100" Background="{Binding State, Converter={StaticResource stateToColorConverter}}"></Grid>
    <Button 
        Width="100" 
        Height="70"
        Command="{Binding InitializeCommand}">Initialize</Button>
</StackPanel>

Its view model has a property State, which has a correct value.

public class MachineControlViewModel :ViewModelBase
{
    private readonly IMachine machine;

    public RelayCommand InitializeCommand { get; set; }

    private MachineStates state;
    public MachineStates State
    {
        get { return state; }
        set { Set(() => State, ref state, value); }
    }

    public MachineControlViewModel(IMachine machine)
    {
        this.machine = machine;

        InitializeCommand = new RelayCommand(Initialize, CanInitialize);

        State = machine.State;

        machine.StateChanged += MachineOnStateChanged;
    }
    // left out irrelevant parts
}

Then, the IValueConverter implementation

public class StateToColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var state = (MachineStates) Enum.Parse(typeof (MachineStates), value.ToString());

        switch (state)
        {
            case MachineStates.Idle:
                return Color.Red;
            case MachineStates.Initialized:
                return Color.Green;
            case MachineStates.Production:
                return Color.Blue;
            case MachineStates.Error:
                return Color.Red;
        }

        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

When I run my app, the backcolor of the Grid will not show.

When I set the BackColor hardcoded (so without the value converter), it is visualized correctly.

When I DO use my value converter, and put a breakpoint in the convert method, I can see that the code executes just fine, and the a Color is returned. But nothing is shown...

What am I doing wrong?

Upvotes: 0

Views: 1615

Answers (2)

Clemens
Clemens

Reputation: 128061

The converter should return a Brush, not a Color, because that is the type of the Background property.

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    Brush brush = null;

    switch ((MachineStates)value)
    {
        case MachineStates.Idle:
            brush = Brushes.Red;
            break;
        case MachineStates.Initialized:
            brush = Brushes.Green;
            break;
        case MachineStates.Production:
            brush = Brushes.Blue;
            break;
        case MachineStates.Error:
            brush = Brushes.Red;
            break;
        default:
            break;
    }

    return brush;
}

It should especially not return a System.Drawing.Color (as you did), because that is WinForms, not WPF.

Upvotes: 1

Maximus
Maximus

Reputation: 3448

Background is of type Brush. Change in Converter to this

 return new SolidColorBrush(Colors.Red);

Background property

Upvotes: 2

Related Questions