There is a basic style that applies to all buttons. I inherit from it another style that will be used only in one button. But then this style interrupts the base one and applies to all buttons.

Is it possible to somehow specify the TargetType property in the derived style so that it does not point to any of the types, or do you have to remove this property from the basic style and explicitly prescribe it for each button?

Basic style:

 <Style x:Key="GeneralButtonStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}"> <Setter Property="Background" Value="Black" /> <Setter Property="MinHeight" Value="15" /> <Setter Property="MinWidth" Value="30" /> <Setter Property="MaxHeight" Value="150" /> <Setter Property="MaxWidth" Value="300" /> <Setter Property="Foreground" Value="Black" /> <Setter Property="Margin" Value="0" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Grid> <Rectangle Name="GelBackground" RadiusX="0" RadiusY="0" Fill="#FFB2B2B2"/> <Rectangle Name="GelShine" Margin="1,1,1,1" RadiusX="0" RadiusY="0"> <Rectangle.Fill> <LinearGradientBrush StartPoint="0,0" EndPoint="0,2"> <GradientStop Offset="0.3" Color="#ccffffff" /> <GradientStop Offset="0.6" Color="Transparent" /> </LinearGradientBrush> </Rectangle.Fill> </Rectangle> <ContentPresenter Name="GelButtonContent" VerticalAlignment="Center" HorizontalAlignment="Center" Content="{TemplateBinding Content}" /> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Rectangle.Fill" TargetName="GelBackground"> <Setter.Value> <RadialGradientBrush> <GradientStop Offset="0" Color="#FF4885CD" /> <GradientStop Offset="1" Color="#FF1B5FF0" /> </RadialGradientBrush> </Setter.Value> </Setter> <Setter Property="Foreground" Value="Black" /> </Trigger> <Trigger Property="IsPressed" Value="True"> <Setter Property="Rectangle.Fill" TargetName="GelBackground"> <Setter.Value> <RadialGradientBrush> <GradientStop Offset="0" Color="#FFFFD39F" /> <GradientStop Offset="1" Color="#FFFF9C00" /> </RadialGradientBrush> </Setter.Value> </Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> 

Derivative:

 <Style x:Key="SearchButtonStyle" BasedOn="{StaticResource GeneralButtonStyle}" > <Setter Property="Control.Tag" Value="1"></Setter> <Style.Triggers> <Trigger Property="Control.Tag" Value="0"> <Setter Property="Button.Content" Value="Остановить поиск"></Setter> </Trigger> <Trigger Property="Control.Tag" Value="1"> <Setter Property="Button.Content" Value="Найти приложения"></Setter> </Trigger> </Style.Triggers> </Style> 
  • You can add a new style x:Key , then it will not apply to all by default. And who needs it, just specify with reference to the key. - Monk
  • I had it so. Now throw the code - Sergey
  • I think I understood, this is not a derivative style interrupted, this is when I added Key to the base, it ceased to act implicitly. Then how to make it act implicitly with Key? Or maybe you can explore the style without Key?) - Sergey
  • You can make a successor without a Key. If the heir is completely unchanged, then it will actually work as a basic style. - Monk
  • And how can I use it without a key? And what does it mean completely unchanged? I have a pair of triggers added to the derivative. - Sergey

1 answer 1

Very simple.

Here is the code:

 <Window.Resources> <Style TargetType="Button"> <Setter Property="Background" Value="#bcd0e5"/> <Setter Property="Foreground" Value="#244e81"/> </Style> <Style TargetType="Button" x:Key="Green" BasedOn="{StaticResource {x:Type Button}}"> <Setter Property="Background" Value="#d3bb9c"/> </Style> </Window.Resources> <StackPanel Orientation="Vertical"> <Button>Стиль по умолчанию</Button> <Button Style="{StaticResource Green}">Перекрытый стиль</Button> </StackPanel> 

gives the result:

There are two buttons, on which default style you will apply, on which one you will block?