Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In WPF Gradient Button pulses between the default color and new gradient once clicked - how to stop

Tags:

wpf

xaml

This is a very annoying artifact I am noticing of WPF and I was curious what I could potentially be doing wrong as I am doing what appears to be simple code in WPF XAML:

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="ExampleShapes.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="400" Height="400">
<Grid x:Name="LayoutRoot">
    <Button Margin="50,50,50,50" Content="I pulse when clicked Why?">
        <Button.Background>
            <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                <GradientStop Color="#FF0C0B0B" Offset="1"/>
                <GradientStop Color="#FFBF5656"/>
            </LinearGradientBrush>
        </Button.Background>
    </Button>
</Grid>

It appears that there are some defaults that are in .NET that I am unaware of with events of the MouseOver and Click that I DO NOT WANT. I don't mind that when the mouse hovers that it changes the color of the gradient to the default but when I remove it it changes back. However even before applying underlying C# code for the 'CLICK' event (not in example) it assumes a 'pulsing' behavior of going between my specified gradient and the default. This is also true with a simple color. What gives?

For further info I am designing on a Windows 7 64 bit box with Visual Studio 2010 and Expression Blend 3 using .NET 4.0 for VS2010 unsure for EB3 as it's an older copy and they Both do this. Is there a simple way to change the default behavior or do I have to set up a new user template for the button?

Any help is much appreciated thanks!

like image 253
djangojazz Avatar asked Dec 08 '11 16:12

djangojazz


People also ask

What is the default value of linear gradient in WPF?

WPF LinearGradientBrush. The default value linear gradient value is diagonal. The StartPoint and EndPoint properties of the LinearGradientBrush represent the start and end points of a gradient. The default values of these properties is (0,0) and (1,1), which is upper-left corner to lower-right corner of an area.

What is a gradientstop?

The GradientStop is the basic building block of a gradient brush. A gradient stop specifies a Color at an Offset along the gradient axis. The gradient stop's Color property specifies the color of the gradient stop.

How do I create partially transparent gradient stops in XAML?

The following sections explain how to create partially transparent gradient stops in XAML and code. In XAML, you use ARGB hexadecimal notation to specify the opacity of individual colors. ARGB hexadecimal notation uses the following syntax:

How do I set the color of a gradient stop?

The gradient stop's Color property specifies the color of the gradient stop. You may set the color by using a predefined color (provided by the Colors class) or by specifying ScRGB or ARGB values. In XAML, you may also use hexadecimal notation to describe a color. For more information, see the Color structure.


1 Answers

It appears to have something to do with the Focused style for the Button. I am guessing the default focused style alternates between the normal and mouseover backgrounds, which is causing the "pulsing" behavior.

Clicking the button sets it as focused, but you can also achieve the same effect by simply tabbing to the button.

Edit

Looking into this a bit more and it seems to be the default for the Aero theme for any element that has input focus. You can set the button's Focusable="False", however then you can't tab to the button or hit Enter on it (can still click it though), and personally I hate applications that I can't navigate with the keyboard.

To avoid the flashing when clicked, you can set focus to another control at the end of your click event, however the effect will still occur when you tab to the button.

like image 123
Rachel Avatar answered Oct 23 '22 04:10

Rachel