简体   繁体   中英

Change Background of Datagrid row depending on datatable Value

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.

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>

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