Question

I am struggling with a seemingly easy problem: I have a datagrid which is bound to a datatable. This Datatable contains a Column named "COLORSTATUS" (an enum value) - I want to paint every ROW of the Datatable depending on the ColorStatus. I have tried to build a value converter - but I am unable to pass it the entire row and / or Datatable. I have thaught about hooking up die DataGridRow events with Caliburns Messages - but how to do this in XAML - all I can acess are DataGrid.RowStyle Elements.

<DataGrid x:Name="excelDataTable_ExcelData"  cal:Message.Attach="[Event AutoGeneratedColumns] = [Action HideTheColorColumn($source)]">
           ?? What to do here
 </DataGrid>

I settled on this solution:

    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Setter Property="Background" Value="{Binding Row, Converter={StaticResource ExcelRowColorConverter}}"></Setter>
        </Style>
    </DataGrid.RowStyle>

I was a little suprised, that you can pass "Row". As a hint for the converter: The passed object is the actual DataRow.

Was it helpful?

Solution

A converter should work fine, but you need to be sure to apply it in the correct spot.

Just pass it the DataRow, get the "COLORSTATUS" column value from it, and return the appropriate color brush.

For example,

<DataGrid.Resources>
    <Style TargetType="{x:Type DataGridRow }">
        <!-- DataContext will be your DataRow -->
        <Setter Property="Background" 
                Value="{Binding Converter={StaticResource MyColorConverter}}" />
    </Style>
</DataGrid.Resources>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top