简体   繁体   中英

Binding to simple properties in WPF DataGrid

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM