loneboat
loneboat

Reputation: 2945

How do I make the selected row highlighted in an unfocused datagrid?

I have a datagrid which I am performing a text search on. When the search determines that the row should be highlighted, I assign a boolean value to "true" in the underlying object, and in my WPF xml I have the following:

        <DataGrid.Style>
            <Style TargetType="DataGrid">
                <Setter Property="AlternatingRowBackground" Value="LightGray"/>
            </Style>
        </DataGrid.Style>
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Highlighted, Mode=TwoWay}" Value="True">
                        <Setter Property="Background" Value="Yellow" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.RowStyle>

Note the "Binding Highlited"; that's how the rows get highlighted. This seems to work fine EXCEPT when a row is selected in the datagrid. When this happens, the selected row turns GRAY when the datagrid loses focus. How can I specify the row color of a SELECTED ROW (meaning "Highlighted" is true) when the datagrid loses focus, so that is still shows a yellow color (maybe darker so it is clear that it's selected)?

Upvotes: 3

Views: 1855

Answers (1)

gomi42
gomi42

Reputation: 2519

Try this:

            <DataGrid.Style>
            <Style TargetType="DataGrid">
                <Setter Property="AlternatingRowBackground" Value="LightGray"/>
            </Style>
        </DataGrid.Style>
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Highlight}" Value="True">
                        <Setter Property="Background" Value="Yellow" />
                    </DataTrigger>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" Value="#FF3CF1C8" />
                        <Setter Property="BorderBrush" Value="#FF3CF1C8" />
                    </Trigger>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Mode=Self}}" Value="True"/>
                            <Condition Binding="{Binding Highlight}" Value="True"/>
                        </MultiDataTrigger.Conditions>
                        <Setter Property="Background" Value="#FFFBAE8A" />
                        <Setter Property="BorderBrush" Value="#FFFBAE8A" />
                    </MultiDataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.RowStyle>
        <DataGrid.CellStyle>
            <Style TargetType="DataGridCell">
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" Value="#FF3CF1C8" />
                        <Setter Property="BorderBrush" Value="#FF3CF1C8" />
                    </Trigger>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridRow}}}" Value="True"/>
                            <Condition Binding="{Binding Highlight}" Value="True"/>
                        </MultiDataTrigger.Conditions>
                        <Setter Property="Background" Value="#FFFBAE8A" />
                        <Setter Property="BorderBrush" Value="#FFFBAE8A" />
                    </MultiDataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.CellStyle>

It keeps the selected row selected in case the DataGrid looses the focus and your highlighted row gets a different selection color which is also kept when having lost the Focus.

Upvotes: 1

Related Questions