Home > database >  Complementing Application.Resources.Style with UserControl.Style for DataRow in DataGrid
Complementing Application.Resources.Style with UserControl.Style for DataRow in DataGrid

Time:02-22

I have a global style used in all the applications DataGrid's, defined in the Application.Resources:

<Application.Resources>
    <ResourceDictionary>
        <Style x:Key="StyleDataGridRow" TargetType="{x:Type DataGridRow}">
            <Setter Property="VerticalAlignment" Value="Center" />
            <Style.Triggers>
                <Trigger Property="DataGrid.IsSelected" Value="True">
                    <Setter Property="DataGrid.BorderBrush" Value="{DynamicResource SecondaryHueMidBrush}" />
                    <Setter Property="DataGrid.BorderThickness" Value="1" />
                    <Setter Property="Background" Value="{DynamicResource PrimaryHueLightBrush}" />
                </Trigger>
                <Trigger Property="DataGrid.IsSelected" Value="False">
                    <Setter Property="DataGrid.BorderBrush" Value="{DynamicResource SecondaryHueMidBrush}" />
                    <Setter Property="DataGrid.BorderThickness" Value="1" />
                </Trigger>
            </Style.Triggers>
        </Style>
</Application:Resources>

In my UserControl I would like to add a style - keeping the global style - to highlight a row depending on an item used in this DataGrid. I could define this style in the UserControl's resources:

    <UserControl.Resources>
        <ResourceDictionary>            
            <Style x:Key="priceMissing" TargetType="{x:Type DataGridRow}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=priceIsMissing}" Value="true">
                        <Setter Property="Background" Value="LightSalmon" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ResourceDictionary>
    </UserControl.Resources>

The global style is applied to the UserControl's DataGrid like this:

<DataGrid x:Name="dgCalculatedServices"
    Margin="20,0,0,0"
    CellStyle="{StaticResource StyleDataGridCell}"
    DataContext="{Binding}"
    ItemsSource="{Binding calculationServiceCodes.collection}"                              
    Style="{StaticResource StyleDataGrid}"
    RowStyle="{StaticResource StyleDataGridRow}">

Does anyone know a way to use the global style on a DataGrid and complement it with the local style?

CodePudding user response:

That is what the BasedOn property is for.

Styles can be based on other styles through this property. When you use this property, the new style will inherit the values of the original style that are not explicitly redefined in the new style.

Specify the StyleDataGridRow style as the base style for priceMissing.

<UserControl.Resources>
    <ResourceDictionary>            
        <Style x:Key="priceMissing" TargetType="{x:Type DataGridRow}" BasedOn="{StaticResource StyleDataGridRow}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=priceIsMissing}" Value="true">
                    <Setter Property="Background" Value="LightSalmon" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ResourceDictionary>
</UserControl.Resources>

Then use the priceMissing style in your DataGrid.

<DataGrid x:Name="dgCalculatedServices
    ...
    RowStyle="{StaticResource priceMissing}">

You could of course rename the priceMissing style to StyleDataGridRow instead. The style would then be overwritten in the scope of the UserControl.

  • Related