CBreeze
CBreeze

Reputation: 2973

Prevent Rectangle Style From Applying to DataGrid

I have a style set in my app.config that I want to colour every rectangle I use in my program. It's nice and simple, it looks like this;

<Style TargetType="Rectangle">
    <Setter Property="Fill" Value="LightBlue"/>
</Style>

I noticed some weird behaviour in all of my DataGrids recently in that when the user navigated through the rows using the arrow keys, the text inside the cell would be blanked out, it looks like this;

BEFORE

enter image description here

AFTER ARROWKEYS PRESSED

enter image description here

As you can see the text is quite clearly gone (that's the same cell in both images). I finally got round to looking at this and came to the conclusion that the rectangle style is applying to each DataGridCell. I can only assume that that is because wpf uses recangles in a DataGridCell.

Is there a way I can apply this style to only the rectangles that I have taken out of the toolbox and not the literal rectangles in the DataGrid?

An easy option out of this is to remove the style completely and individually apply it, but if we change the colour scheme further down the line this could become very time consuming.

UPDATED

I'd also (if possible) like to use any fix in conjuction with modifying my DataGridCell appearance which I do like so;

<Style TargetType="DataGridCell">
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="Orange" />
            <Setter Property="BorderBrush" Value="Black" />
        </Trigger>
    </Style.Triggers>
</Style>

Upvotes: 2

Views: 71

Answers (1)

ASh
ASh

Reputation: 35703

DataGridCells use FocusVisualStyle which has Rectangle in its visual tree. Those rectangles get Fill brush from default Rectangle style. If you change FocusVisualStyle, cells become visible on keyboard selection

<DataGrid.CellStyle>
    <Style TargetType="{x:Type DataGridCell}">
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    </Style>
</DataGrid.CellStyle>

if there is already a default style for DataGridCell, add a setter there

<Style TargetType="DataGridCell">
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/> 
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="Orange" />
            <Setter Property="BorderBrush" Value="Red" />
        </Trigger>
    </Style.Triggers>
</Style>


additional info: I have the same issue with Buttons and selection using Tab

Upvotes: 2

Related Questions