0

I've got a problem binding to a property on my model. In a DataGrid, I'm displaying Errors. Each error has the property ErrorDescription which itself has the property Severity.

I can bind to Severity in the TextColumn of my DataGrid below, however binding to Severity in the TemplateColumn fails with the error

"Cannot resolve property "ErrorDescription" in data context of type MainViewModel"

The DataContext my image-column is not the same as in my first text-column. Why is that?

  <DataGrid ItemsSource ="{Binding Errors}" AutoGenerateColumns="False">
        <DataGrid.Columns>

            // works
            <DataGridTextColumn Binding="{Binding ErrorDescription.Severity}"></DataGridTextColumn>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Image>
                            <Image.Style>
                                <Style TargetType="Image">
                                    <Style.Triggers>
                                       // Binding fails
                                        <DataTrigger Binding="{Binding ErrorDescription.Severity}" Value="Unknown">
                                            <Setter Property="Source" Value="/error.jpg"/>
                                        </DataTrigger>
                                        // Binding fails
                                        <DataTrigger Binding="{Binding ErrorDescription.Severity}" Value="Ok">
                                            <Setter Property="Source" Value="/ok.jpg"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </Image.Style>
                        </Image>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
      </DataGrid>
peter
  • 2,103
  • 7
  • 25
  • 51

1 Answers1

1

What about using a Converter instead of DataTrigger? When I want to show icons or specific property values depending an enum I do it with a Converter.

The Converter would be similar to this:

public class ErrorSeverityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        Severity severity = (Severity)value;
        switch(severity)
        {
            case Severity.Unknown:
                return "/error.jpg";

            case Severity.Ok:
                return "/ok.jpg";
        }
    }

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

There is this other question that is very similar even if not on a DataGridTemplateColumn

Enable TextBox depending on enum value

Community
  • 1
  • 1
Origence
  • 155
  • 3
  • 10
  • Severity is indeed an enum, however, I can display its value in the TextColumn without any further ado, but I cannot use get its value in the TemplateColumn. I'll adjust my post. – peter Aug 27 '15 at 07:51