简体   繁体   中英

WPF- Button sunken effect

I have a button in c# and I have added a gradient brush to it to create a sunken effect when the button is pressed. It works by increasing the border thickness (top left and top) when pressed. I couldn't get it to work. Is there something I am missing?

My XAML:

<menu:HomeButton x:Name="BtnHome" MouseDown="BtnHome_MouseDown"  >
                    <menu:HomeButton.Background>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="Black" Offset="0"/>
                            <GradientStop Color="White" Offset="1"/>
                        </LinearGradientBrush>
                    </menu:HomeButton.Background>

I've made an event handler for this button(in c#) :

 private void BtnHome_MouseDown(object sender, MouseButtonEventArgs e)
        {
            BtnHome.BorderThickness = new Thickness(5, 5, 5, 5);
        }

So when I pressed the button, nothing happens. What did i miss?

Probably some quirk with the routing order/strategy of the MouseDown routed event vs when WPF decides to redraw. With a regular button (I'm assuming HomeButton is a derivation of the standard WPF Button) I'm unable to change the border thickness on MouseDown as you have demonstrated. However, if I use the PreviewMouseDown event instead, it works.

    <Button x:Name="TheButton" PreviewMouseDown="Button_PreviewMouseDown">YOOOO</Button>

   private void Button_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        TheButton.BorderThickness = new Thickness(5, 5, 5, 5);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM