仅当设置了特性时才应用样式

本文关键字:应用 样式 设置 | 更新日期: 2023-09-27 18:21:17

我有下面的样式,它应用于我的窗口中的所有按钮。

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

但我需要使用整个样式,只有当属性"BorderThickness"设置为零时。否则,按钮应具有默认样式。

仅当设置了特性时才应用样式

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

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

虽然answear已经被接受,但我想分享我的解决方案,请看一看。它使用混合名称空间(交互/交互)。

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