1

I'm new to WPF and have a question among behavior in forms.

I added a key down event to catch a pressed return key.

Now the Problem, there is no visual feedback. Is it possible to let the button look like it got pressed by the mouse?

It should look like i pressed the button via mouse, but instead after pressing return.

4

2 Answers 2

2

If I understand, you want to simulate the visual effect of a button click, so, to achieve this, you have to change the IsPressed property of the button, but is read-only, then, System.Reflection is useful:

on KeyDown event:

typeof(Button).GetMethod("set_IsPressed", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(btnSimulate, new object[] { true });

and KeyUp:

typeof(Button).GetMethod("set_IsPressed", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(btnSimulate, new object[] { false });
2

Maybe the solution will go by changing the KeyPressed event for the button.

See this code example:

At first, create a button:

<ButtonContent="ButtonPressed" Name="buttonPressed" />

Then you should add Resources to your main.xaml that will tell the button how it will look (like CSS in Web Pages).

<Window.Resources>
 <Style TargetType="Button">
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="Foreground" Value="White" />
        <Setter Property="Background" Value="{StaticResource GlowFX}" />
        <Setter Property="Width" Value="150" />
        <Setter Property="Height" Value="35" />
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="{StaticResource GlowFXPressed}" />
            </Trigger>
        </Style.Triggers>
</Style>

With the Background property I'm changing the default Background for the button. The Trigger launches when you are over your button, and, as you can see, it changes again the background, with these two styles NOTE: paste them before the Button style:

<RadialGradientBrush x:Key="GlowFX" GradientOrigin=".5,1" Center=".5,1">
        <GradientStop Offset="0" Color="#990000FF"></GradientStop>
        <GradientStop Offset=".3" Color="#660000DD"></GradientStop>
        <GradientStop Offset="1" Color="#33000000"></GradientStop>
    </RadialGradientBrush>
    <RadialGradientBrush x:Key="GlowFXPressed" GradientOrigin=".5,1" Center=".5,1">
        <GradientStop Offset="0" Color="#990000FF"></GradientStop>
        <GradientStop Offset=".3" Color="#880000DD"></GradientStop>
        <GradientStop Offset="1" Color="#33000000"></GradientStop>
    </RadialGradientBrush>

This works with the event IsMoseOver, but I think it would be the same for keypressed. Only look a little bit ;)

As Rachel says, you should rewrite the full template, and tell the Press event what to do with styles.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.