0

I have a styled window and I want to override a datagrid style to ALL datagrids in my application

    <Window.Resources>          
    <Style x:Name="dtgStyle" TargetType="{x:Type DataGridRow}">
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Transparent" />
            </Trigger>

        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="Blue" />
        </Trigger>

            <Trigger Property="IsSelected" Value="False">
                <Setter Property="Background" Value="Transparent" />
            </Trigger>
        </Style.Triggers>

        <Setter Property="Background" Value="Transparent" />
        <Setter Property="Foreground" Value="White" />

    </Style>
</Window.Resources>

I thougth this had to work but I have to apply

Style s = Resources["dtgStyle"] as Style;
mydtg.Style = s;

now I wouldn't like to have to apply that to ALL dtgs. The best would be to automatically apply it in xaml.

Thanx

---ADD for ASh----

Thank you for your help. The only problem is that when the datagrid loses focus the selected line in the datagrid changes colour as you can see in the following pic (foreground turns to black).

enter image description here

I have tried to add various property but nothing works.

additionally the left border gets bolder (no pun intended) and larger. Any idea how to fix it? Thanks

Patrick
  • 3,073
  • 2
  • 22
  • 60
  • `TargetType="{x:Type DataGridRow}"`. it is DataGridRow style, not DataGrid. And it should applied by default because there is not explicit key – ASh Jul 08 '16 at 10:26
  • Thanx so I changed the DatagridRow name to dtrStyle and addded but how to add the proper style now? – Patrick Jul 08 '16 at 10:41
  • @Patrick The way you set `TargetType="{x:Type DataGridRow}"`, you can set for `DataGrid` . Whats the big deal ? What u r trying to achieve ? – AnjumSKhan Jul 08 '16 at 10:48

1 Answers1

3

if you need default style for FrameworkElement, declare it without x:Key, only with TargetType.

Both DataGridRow and DataGridCell have IsSelected property. Not enough to change Background only for DataGridRow, it has to be done for DataGridCell as well

<Style TargetType="{x:Type DataGrid}">
    <Setter Property="RowStyle" >
        <Setter.Value>
            <Style TargetType="{x:Type DataGridRow}" BasedOn="{StaticResource {x:Type DataGridRow}}">
                <Setter Property="Background" Value="Transparent" />
                <Setter Property="Foreground" Value="White" />

                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="Transparent" />
                    </Trigger>

                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" Value="Orange" />
                        <Setter Property="Foreground" Value="White" />
                    </Trigger>
                </Style.Triggers>

            </Style>
        </Setter.Value>
    </Setter>

    <Setter Property="CellStyle">
        <Setter.Value>
            <Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {x:Type DataGridCell}}">
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" Value="Orange" />
                    </Trigger>

                    <DataTrigger Binding="{Binding IsKeyboardFocusWithin, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Value="False">
                        <Setter Property="Foreground" Value="White"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Setter.Value>
    </Setter>
</Style>

fix for Foreground was found here: DataGrid's selected row color when inactive (https://stackoverflow.com/a/25204493/1506454)

Community
  • 1
  • 1
ASh
  • 34,632
  • 9
  • 60
  • 82