样式中设置的触发器无法正常工作

问题描述 投票:0回答:2

我创建了一个类库,其中包含几乎每个用户控件的样式。在我的应用程序中,我将此样式库添加为nuget包。

    <Style TargetType="{x:Type Button}" x:Key="tkButton">
    <Setter Property="Background" Value="{StaticResource exQuiteDarkBrush}"/>
    <Setter Property="Foreground" Value="{StaticResource tkLightGrayBrush}"/>
    <Setter Property="FontFamily" Value="{StaticResource TKTypeRegular}"/>
    <Setter Property="BorderBrush" Value="{StaticResource tkMediumGrayBrush}"/>
    <Setter Property="MinHeight" Value="25"/>
    <Setter Property="MinWidth" Value="60"/>
    <Setter Property="Margin" Value="2"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="BorderBrush" Value="{StaticResource tkBrandBlueBrush}"/>
            <Setter Property="Foreground" Value="{StaticResource tkBrandBlueBrush}"/>            
            </Trigger>
        <Trigger Property="UIElement.IsEnabled" Value="False">
            <Setter Property="Foreground" Value="{StaticResource tkMediumGrayBrush}"/>
            <Setter Property="BorderBrush" Value="{StaticResource tkDarkGrayBrush}"/>
        </Trigger>
    </Style.Triggers>
</Style>

例如,该库包含一个简单的按钮。在大多数用户控件中,我使用触发器为IsMouseOver属性更改一些颜色。在我的应用程序中,我使用的是这样的样式:

<Button Style="{DynamicResource tkButton}" Content="button with ITALIC and BOLD type" FontStyle="Italic" FontWeight="Bold" IsEnabled="{Binding Enabled}"/>
<Button BorderBrush="{StaticResource tkRedBrush}" Style="{DynamicResource tkButton}" Content="button with ITALIC type" FontStyle="Italic" IsEnabled="{Binding Enabled}"/>

第一个按钮将添加样式,并将其悬停在预期的位置。第二个使用样式,但是将BorderBrush更改为红色。将鼠标悬停在第二个按钮上时,我希望更改BorderBrush,但它保持红色。

我能想到的唯一解决方案是再次在窗口/应用程序内部设置触发器。是否有其他解决方案来更改此行为?

c# wpf user-controls
2个回答
0
投票

将触发器移至ControlTemplate

<ControlTemplate TargetType="{x:Type Button}">
    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="BorderBrush" Value="{StaticResource tkBrandBlueBrush}"/>
            <Setter Property="Foreground" Value="{StaticResource tkBrandBlueBrush}"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

如果将其放在Style中,则由BorderBrush="{StaticResource tkRedBrush}"指定的本地值将优先于由Setter设置的值:https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/dependency-property-value-precedence


0
投票

您可以尝试通过以下方式声明按钮

<Button Content="button with ITALIC type" FontStyle="Italic" IsEnabled="{Binding Enabled}">
    <Button.Style>
        <Style TargetType="Button" BasedOn="{DynamicResource tkButton}">
            <Setter Property="BorderBrush" Value="{StaticResource tkRedBrush}"/>
        </Style>
    </Button.Style>
</Button>
© www.soinside.com 2019 - 2024. All rights reserved.