How to avoid flashing button WPF

11,074

Solution 1

Firstly, you could try setting the button Focusable to False. However, this will be a problem if you use keyboard navigation. I did however find this Window.Resource XAML code:

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

Then whenever you want to apply the custom button style, set the button Style to:

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

Solution 2

You can override the Button template to remove the default Themed(Aero) style

Something like this will be a good place to start.

<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>
Share:
11,074
Babak.Abad
Author by

Babak.Abad

A dedicated AI programmer with a focus on machine vision applications I develop apps and optimize tasks with help of machine learning. I can learn machines and optimize them to do tasks such as reading texts, classifying objects, recognizing voice, or even read emotions from biosignals. As a CTO, I manage my team to develop artificial intelligence (AI) solutions. I determine solution requirements including, computers, cameras, bandwidth, etc. As a programmer, I can develop machine learning apps using multiple programming languages. My current objectives include developing speed enforcement cameras. My experience includes developing real-time automatic license plate recognition (ALPR) system. I developed a license plate detector, OCR pipeline, and camera synchronizer (an electronic board). I also developed an RFID-tag-based system as a customer management system. artificial intelligence | machine learning | machine vision | deep learning | intelligent transportation | OCR | C# | Python | Matlab

Updated on June 08, 2022

Comments

  • Babak.Abad
    Babak.Abad almost 2 years

    I'm writing a program using WPF(c#). I use several buttons. Anny button start flashing after clicking on it! Its color fading from background color to aqua (light blue) color. It does not stop flashing while I click another button( then this button starts flashing!!!). Is it a bug? is it related to my visual studio?

    How can I fix this problem?

  • Faizan Mubasher
    Faizan Mubasher over 10 years
    Microsoft_Windows_Themes:ButtonChrome giving error message. It is Micorsoft.Windows.Themes:ButtonChrome. Its not working. It gives me message that ButtonChrome is not supported in WPF project.