Extending Default Style of Parent Control in CustomUserControl not working

Viewed 57

I am trying to extend an implicit Style of a control which is defined in any parent control within a custom UserControl. This doesn't seem to work as I would expect.

The scenario would be: Including a ResourceDictionary in MainWindow.xaml which defines a default theme. Than I want to extend a base Style which is defined in that theme within my UserControl.

What I have already tried:

Having a MainWindow.xaml which defines the following DefaultStyle in its Resources:

<Window x:Class="UserControl_Style_Inheritance.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:UserControl_Style_Inheritance"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
  <Window.Resources>
    <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
      <Setter Property="Background" Value="Blue"></Setter>
    </Style>
  </Window.Resources>
  <Grid>
    <local:MyUserControl></local:MyUserControl>
  </Grid>
</Window>

And having a MyUserControl.xaml which should extend the defined DefaultStyle of MainWindow.xaml:

<UserControl x:Class="UserControl_Style_Inheritance.MyUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:UserControl_Style_Inheritance"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
  <UserControl.Resources>
    <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
      <Setter Property="BorderBrush" Value="Red"></Setter>
    </Style>
  </UserControl.Resources>
  <Grid>
      <Button>Hello</Button> 
  </Grid>
</UserControl>

But MyUserControl is based on the normal Button Style by having a gray background instead of a blue background of the Style defined in MainWindow.xaml.

What am I doing wrong, or where am I thinking into the wrong direction?

Edit: Putting the default Style into the Application.xaml file works, but this is not what I want to do.

1 Answers

Try adding the button style to App.xaml:

<Application.Resources>
    <Style x:Key="BtnStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
        <Setter Property="Background" Value="Blue"></Setter>
    </Style>
</Application.Resources>

And use it in your UserControl like this:

<UserControl.Resources>
    <Style TargetType="{x:Type Button}" BasedOn="{StaticResource BtnStyle}"> 
        <Setter Property="BorderBrush" Value="Red"></Setter>
    </Style>
</UserControl.Resources>
<Grid>
    <Button>Hello</Button>
</Grid>
Related