Search code examples
wpfwpfdatagrid

Datagrid Style in xaml wpf


Here's my datagrid code,

<DataGrid Name="dtLogView"
   Margin="10,10,10,10"
   ItemsSource="{Binding}"
   **RowBackground="Gray"
   AlternatingRowBackground="LightGray"**
</Datagrid>

Now, how to include the last 2 properties in Style tag. For instance,

<Style x:Key="{x:Type DataGridRow}" TargetType="{x:Type DataGridRow}">
  <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" />
<Setter Property="AlterationBackground" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" />
</Style>

Thanks in advance.


Solution

  • You should name the key something meaningful, call it StyleX for now. Use that name with the Style property in your grid.

    If you omit the key name, it will be seen as default style for that target type.

    You can use the same properties for your Style, but put them in the Property and Value properties. Don't use DynamicResource for Static properties.

    The code you need:

    <Style TargetType="{x:Type DataGridRow}">
      <Setter Property="Background" Value="{StaticResource {x:Static SystemColors.WindowBrushKey}}" />
    <Setter Property="AlterationBackground" Value="{StaticResource {x:Static SystemColors.WindowBrushKey}}" />
    </Style>