仅在设置了属性时才应用样式

时间:2014-09-08 20:16:00

标签: c# wpf

我有以下风格,适用于我窗口中的所有按钮。

<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>

但我需要使用这整个风格,只有属性&#34; BorderThickness&#34;设置为零。否则,该按钮应具有默认样式。

2 个答案:

答案 0 :(得分:2)

您可以使用触发器,这是一个完整的示例:

<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>

答案 1 :(得分:0)

虽然已经接受了answear,但我想分享我的解决方案,看一看。它使用Blend名称空间(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>

和MyClass看起来如下

  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;
    }
}