如何避免WPF中按钮的闪烁问题

7

我正在使用WPF(c#)编写程序。我使用了几个按钮。任何一个按钮在点击后都会开始闪烁!它的颜色从背景色渐变到水色(浅蓝色)。当我点击另一个按钮时,它不会停止闪烁(而是这个按钮开始闪烁!!!)。这是一个bug吗?这是否与我的Visual Studio有关?

我该如何解决这个问题?


2
这是按钮的默认视觉样式,Windows 已经使用了很多年,这不是新的。当按钮获得焦点时会出现这种情况,如果你想停止按钮执行此操作,你可以重写按钮模板或简单地取消按钮的焦点。 - sa_ddam213
2个回答

2
您可以覆盖Button模板来删除默认的Themed(Aero)样式。以下内容是一个不错的起点。
<Style TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border x:Name="Border" CornerRadius="2" BorderThickness="1" Background="{x:Static SystemColors.ControlBrush}" BorderBrush="{x:Static SystemColors.ControlDarkDarkBrush}">
                    <ContentPresenter  Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsKeyboardFocused" Value="True">
                        <Setter TargetName="Border" Property="BorderBrush" Value="{x:Static SystemColors.ControlBrush}" />
                        <Setter TargetName="Border" Property="BorderBrush" Value="{x:Static SystemColors.ControlDarkDarkBrush}" />
                    </Trigger>
                    <Trigger Property="IsDefaulted" Value="True">
                        <Setter TargetName="Border" Property="BorderBrush" Value="{x:Static SystemColors.ControlBrush}" />
                        <Setter TargetName="Border" Property="BorderBrush" Value="{x:Static SystemColors.ControlDarkDarkBrush}" />
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="Border" Property="Background" Value="{x:Static SystemColors.ActiveCaptionBrush}" />
                        <Setter TargetName="Border" Property="BorderBrush" Value="{x:Static SystemColors.ControlDarkDarkBrush}" />
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter TargetName="Border" Property="Background" Value="{x:Static SystemColors.ActiveCaptionBrush}" />
                        <Setter TargetName="Border" Property="BorderBrush" Value="{x:Static SystemColors.ControlDarkDarkBrush}" />
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter TargetName="Border" Property="Background" Value="#EEEEEE" />
                        <Setter TargetName="Border" Property="BorderBrush" Value="#AAAAAA" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

2

首先,您可以尝试将按钮的Focusable属性设置为False。但是,如果您使用键盘导航,这会成为一个问题。但我找到了这个Window.Resource XAML代码:

<Window.Resources>
<Style x:Key="ButtonFocusVisual">
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate>
                <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<LinearGradientBrush x:Key="ButtonNormalBackground" EndPoint="0,1" StartPoint="0,0">
    <GradientStop Color="#F3F3F3" Offset="0"/>
    <GradientStop Color="#EBEBEB" Offset="0.5"/>
    <GradientStop Color="#DDDDDD" Offset="0.5"/>
    <GradientStop Color="#CDCDCD" Offset="1"/>
</LinearGradientBrush>
<SolidColorBrush x:Key="ButtonNormalBorder" Color="#FF707070"/>
<Style x:Key="BoringButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
    <Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
    <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Padding" Value="1"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Microsoft_Windows_Themes:ButtonChrome x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}" RenderDefaulted="{TemplateBinding IsDefaulted}" SnapsToDevicePixels="true">
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Microsoft_Windows_Themes:ButtonChrome>
                <ControlTemplate.Triggers>
                    <Trigger Property="ToggleButton.IsChecked" Value="true">
                        <Setter Property="RenderPressed" TargetName="Chrome" Value="true"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="#ADADAD"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

每当您想应用自定义按钮样式时,请将按钮Style设置为:

<Button Style="{DynamicResource BoringButtonStyle}"/>

Microsoft_Windows_Themes:ButtonChrome出现错误信息。应该是Micorsoft.Windows.Themes:ButtonChrome。它无法正常工作。它会提示我在WPF项目中不支持ButtonChrome。 - Faizan Mubasher

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接