微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当 IsPress=True 时,按钮控件样式会更改背景,但永远不会变回原始颜色

如何解决当 IsPress=True 时,按钮控件样式会更改背景,但永远不会变回原始颜色

我在 XAML 资源字典中定义了一个应用程序认按钮样式。 ControlTemplate 包含 Ispressed="True" 的触发器,它会在按钮被按下时更改按钮背景。

但是,在按下并释放按钮后,在Ispressed="True"期间设置的背景,背景颜色永远不会变回原来的背景颜色。

这是在 VS 2019 和 .NET core v3.1 中。

按钮 xaml 是:

<Button x:Name="btnHelp" Width="50" Height="25" Content="_Help" Click="btnHelp_Click"  BorderBrush="AliceBlue" Margin="10,0" HorizontalAlignment="Center" VerticalAlignment="Center"/>

这是我的资源字典中的按钮样式定义:

    <Style targettype="{x:Type Button}">
    <Setter Property="Padding" Value="1" />
    <Setter Property="Foreground" Value="{DynamicResource TextBrush}" />
    <Setter Property="Background" Value="{DynamicResource ButtonBackgroundBrush}" />
    <Setter Property="BorderBrush" Value="{DynamicResource BaseBorderBrush}" />
    <Setter Property="BorderThickness" Value="{DynamicResource ButtonBorderThickness}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate targettype="{x:Type Button}">
                <Border
                    x:Name="outerborder"
                    Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}"
                    ClipToBounds="True"
                    CornerRadius="{DynamicResource ButtonCornerRadius}">
                    <Grid>
                        <Border
                            Name="glow"
                            Margin="{DynamicResource GlowBorderMargin}"
                            BorderBrush="{DynamicResource ButtonGlowBrush}"
                            BorderThickness="{DynamicResource GlowBorderThickness}"
                            CornerRadius="{DynamicResource ButtonCornerRadius}"
                            Effect="{DynamicResource ButtonHoverGlowEffect}"
                            Visibility="Collapsed" />
                        <ContentPresenter
                            x:Name="content"
                            Margin="{TemplateBinding Padding}"
                            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                            RecognizesAccessKey="True"
                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouSEOver" Value="True">
                        <Setter Property="BorderBrush" Value="{DynamicResource SelectedBrush}" />
                        <Setter Property="Background" Value="{DynamicResource MenuHoverBrush}" />
                        <!--<Setter TargetName="glow" Property="Visibility" Value="Visible" />-->
                    </Trigger>
                    <Trigger Property="Ispressed" Value="True">
                        <Setter Property="Background" Value="{DynamicResource ButtonBackgroundpressedBrush}" />
                        <Setter Property="BorderBrush" Value="{DynamicResource DarkerSelectedBrush}" />
                        <Setter TargetName="glow" Property="Effect" Value="{DynamicResource ButtonpressedGlowEffect}" />
                        <Setter TargetName="glow" Property="Visibility" Value="Visible" />
                    </Trigger>
                    <Trigger Property="IsFocused" Value="True" >
                        <Setter Property="BorderBrush" Value="{DynamicResource SelectedBrush}" />
                        <Setter Property="Background" Value="{DynamicResource MenuHoverBrush}" />
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Background" Value="Transparent" />
                        <Setter TargetName="outerborder" Property="BorderBrush" Value="{DynamicResource disabledBorderBrush}" />
                        <Setter Property="Foreground" Value="{DynamicResource disabledBrush}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

解决方法

罪魁祸首是 IsFocused 触发器中的以下行。当您单击 Button 时,它会聚焦,因此触发器将设置按钮的 Background。如果您将焦点移动到另一个控件,例如按Tab,背景会变回正常。由于您对 PressedFocused 状态使用相同的背景,它似乎停留在 Pressed 状态,但专注

<Setter Property="Background" Value="{DynamicResource MenuHoverBrush}" />

从触发器中移除该行,您的按钮将按预期工作。

<Trigger Property="IsFocused" Value="True" >
    <Setter Property="BorderBrush" Value="{DynamicResource SelectedBrush}" />
</Trigger>
,

那是因为您触发了设置背景属性的事件,并且不再触碰它。 为了实现您的意图,您必须为“IsPressed”设置另一个触发器并将背景设置为您想要的任何内容。

这是示例片段:

<Trigger Property="IsPressed" Value="True">
    <Setter Property="Background" Value="Bisque" />
    <Setter Property="BorderBrush" Value="Bisque" />
</Trigger>
<Trigger Property="IsPressed" Value="False">
    <Setter Property="Background" Value="AliceBlue" />
    <Setter Property="BorderBrush" Value="AliceBlue" />
</Trigger>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。