0

I am populating a Datagrid from a datatable. The datatable is created at runtime, and there are an unknown number of rows and columns. The column headers are created when the datatable is loaded. If there is an empty cell in the Datatable I would like it highlighted.

I looked at the answer here. But it does not work, It only highlights a cell in a single column, and they are binding to a column. Which I can't do.

My code:

<DataGrid x:Name="dataGrid" ItemsSource="{Binding DGLines}">
        <DataGrid.Columns>
            <DataGridTextColumn>
                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="{x:Type TextBlock}">
                        <Style.Triggers>
                            <Trigger Property="Text" Value="">
                                <Setter Property="Background" Value="LightGreen"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

The empty cells value is actually null. I'm not sure if that's the problem.

What is the correct style to highlight an empty cell in any column?

  • Yes I did. I marked as accepted, and upvoted. Posted a thanks comment. But the comment was removed, as it did not meet the guidelines for comments. –  Aug 09 '18 at 12:05

1 Answers1

3

Are you using a UserControl or Window? If it's Window then use this

   <Window.Resources>
        <Style  TargetType="{x:Type DataGridCell}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Content.Text, RelativeSource={RelativeSource Self}}"  Value="" >
                    <Setter Property="Background" Value="LightGreen"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>

Else if it's UserControl just replace Window.Resources with UserControl.Resources. I believe that similar answer was already asked, but I couldn't find it.

sharp
  • 1,191
  • 14
  • 39