Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set style for custom control

Tags:

c#

wpf

xaml

I'm trying to create a custom user control. I've created ResourceDictionary file (Themes\Generic.xaml) with two styles:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit"
    xmlns:themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
    xmlns:components="clr-namespace:ORPO.WPF.Components">

    <Style TargetType="{x:Type components:HeaderFilterDataGrid}" BasedOn="{StaticResource {x:Type DataGrid}}">
        ...
    </Style>
    <Style TargetType="{x:Type DataGridColumnHeader}">
        ...
    </Style>
</ResourceDictionary>

and my custom control class:

public class HeaderFilterDataGrid : DataGrid
    {
...
static HeaderFilterDataGrid()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(HeaderFilterDataGrid),
new FrameworkPropertyMetadata(typeof(HeaderFilterDataGrid)));            
        }
...
}

It works fine when I apply first style

DefaultStyleKeyProperty.OverrideMetadata(typeof(HeaderFilterDataGrid),
    new FrameworkPropertyMetadata(typeof(HeaderFilterDataGrid)));   

How can i apply second style for my custom control? I need both styles to be applied at the same time.

like image 865
Stopee Avatar asked Jun 01 '26 19:06

Stopee


1 Answers

Put the DataGridColumnHeader Style in the Resources of the HeaderFilterDataGrid Style. This way DataGridColumnHeader will be a default style for all DataGridColumnHeaders in a HeaderFilterDataGrid.

<ResourceDictionary ...>
    <Style TargetType="{x:Type components:HeaderFilterDataGrid}"
           BasedOn="{StaticResource {x:Type DataGrid}}">
        <Style.Resources>
            <Style TargetType="{x:Type DataGridColumnHeader}">
            ...
            </Style>
        </Style.Resources>
        ...
    </Style>
</ResourceDictionary>
like image 98
Clemens Avatar answered Jun 03 '26 09:06

Clemens