Creating a custom control without changing the parent template

36 Views Asked by At

I read several topics and discussions on this issue, but I could not decide which implementation option is optimal.

Initial conditions (example for clarification):

  1. Created a new solution "WPF .Net 6";

  2. Downloaded the NuGet packages "MaterialDesignThemes" and "MaterialDesignThemes.MahApps";

  3. Selected the "Default" theme:

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
    
  4. “Manually” created an empty custom control derived from ComboBox.

    public class MyComboBox : ComboBox { }
    
  5. Expected behavior: Since the default style key was not reassigned, the ComboBox styles should be automatically picked up.

  6. Parent styles are not picked up. I have to explicitly define a style that is derived from the ComboBox style. This can be done in different ways. One of the possible options:

    <UniformGrid Background="Aqua" Rows="1" Height="100">
        <ComboBox Margin="10" 
                  BorderThickness="2" BorderBrush="Green"/>
        <local:MyComboBox Margin="10" 
                  BorderThickness="2" BorderBrush="Green"/>
        <local:MyComboBox Margin="10" 
                  BorderThickness="2" BorderBrush="Green">
            <FrameworkElement.Style>
                <Style TargetType="local:MyComboBox" BasedOn="{StaticResource {x:Type ComboBox}}"/>
            </FrameworkElement.Style>            
        </local:MyComboBox>
    </UniformGrid>
    

enter image description here

Another solution:

public class MyComboBox : ComboBox
{
    public MyComboBox()
    {
        SetResourceReference(StyleProperty, typeof(ComboBox));
    }
}

Not working variants.
A) Setting the default key by parent type:

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

B) Setting a style in Themes/Generic.xaml derived from the parent one:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:cc="clr-namespace:WpfCustomControlMD">
    <Style TargetType="{x:Type cc:MyComboBox}"
           BasedOn="{StaticResource {x:Type ComboBox}}"/>
</ResourceDictionary>

As I understand it, a custom control automatically uses the style of the parent element specified in the assembly of the parent element (in its theme), but for some reason it cannot automatically use the default styles of the parent type specified in the application.

What are the options for solving this problem “Creating a custom control that automatically uses the default styles specified in the application”?

0

There are 0 best solutions below