2

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>
CBreeze
  • 2,925
  • 4
  • 38
  • 93

1 Answers1

2

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
ASh
  • 34,632
  • 9
  • 60
  • 82
  • Hi ash, I've updated my question. Your fix does work in keeping the text however it (as I would expect) removes the style that I would like to apply to the `DataGridCell`. Is there a way of being able to do both? – CBreeze Jun 01 '16 at 09:49
  • Apologies for bringing up an old (ish) thread.. but now when I am doing something such as `dataGrid.Focus()` the entire `DataGrid` is being filled LightBlue. I assume again that rectangle is used within `DataGrid` somehow, is there a way to prevent this from happening? Also as you stated in your answer I am also having this issues when tabbing through `Buttons` as well. How did you manage to get around the `Button` issue? – CBreeze Jun 14 '16 at 09:36
  • @CBreeze, looks like it is their `FocusVisualStyle` again, which is basically a rectangle, but *without default Fill value*. this post may be helpful: http://stackoverflow.com/questions/1055670/deactivate-focusvisualstyle-globally – ASh Jun 14 '16 at 10:44