Reputation: 4425
I have the follow style, that is applied to all buttons in my window.
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border x:Name="Overlay">
<ContentPresenter/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="Overlay" Property="Background" Value="Transparent"/>
</Trigger>
<Trigger Property="IsEnabled" Value="True">
<Setter TargetName="Overlay" Property="Background" Value="Transparent"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Overlay" Property="Background" Value="{StaticResource MouseOver}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
But I need to use this entire style, only if the property "BorderThickness" is set to zero. Otherwise, the button should have default style.
Upvotes: 2
Views: 123
Reputation: 3448
Although answear is already accepted I want to share my solution, take a look. It uses Blend namespaces(Interaction/Interactivity).
<Button BorderThickness="0" Content="xD">
<Button.Resources>
<Style x:Key="Style1" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Background="CadetBlue">
<ContentPresenter/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Button.Resources>
<i:Interaction.Triggers>
<is:DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=BorderThickness}" Value="0">
<local:MyClas TargetObject="{Binding RelativeSource={RelativeSource AncestorType=Button}}" MyStyle="{StaticResource Style1}"/>
</is:DataTrigger>
<is:DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=BorderThickness}" Value="1">
<local:MyClas TargetObject="{Binding RelativeSource={RelativeSource AncestorType=Button}}" MyStyle="{x:Null}"/>
</is:DataTrigger>
</i:Interaction.Triggers>
</Button>
and MyClass looks as follows
public class MyClas : TargetedTriggerAction<Button>
{
public static readonly DependencyProperty MyStyleProperty = DependencyProperty.Register("MyStyle", typeof(Style), typeof(MyClas), new PropertyMetadata());
public Style MyStyle
{
get { return (Style)GetValue(MyStyleProperty); }
set { SetValue(MyStyleProperty, value); }
}
protected override void Invoke(object parameter)
{
((Button)Target).Style = MyStyle;
}
}
Upvotes: 0
Reputation: 13354
You could use a trigger, here is a full sample:
<Window x:Class="ButtonsStyle.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<Trigger Property="BorderThickness" Value="0">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border x:Name="Overlay">
<ContentPresenter/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="Overlay" Property="Background" Value="Transparent"/>
</Trigger>
<Trigger Property="IsEnabled" Value="True">
<Setter TargetName="Overlay" Property="Background" Value="Transparent"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Overlay" Property="Background" Value="Red" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
<Button>Hello</Button>
<Button BorderThickness="0">World</Button>
</StackPanel>
</Window>
Upvotes: 2