#665 – Reacting to MouseEnter / MouseLeave Events

If there’s some action that you want to take when a user hovers over a control, you can add handlers for the MouseEnter and MouseLeave events.

For example, let’s say that you want to change a Button’s text when the user hovers over the button.  You could define handlers for the MouseEnter and MouseLeave events in XAML:

    <StackPanel Margin="20" >
        <Button Content="Click Me" HorizontalAlignment="Center" Padding="10,5"
                MouseEnter="Button_MouseEnter_1" MouseLeave="Button_MouseLeave_1"/>

Then, in your handlers, you could set (or restore) the Button’s Content property.

        private void Button_MouseEnter_1(object sender, MouseEventArgs e)
            ((Button)sender).Content = "CLICK Me";

        private void Button_MouseLeave_1(object sender, MouseEventArgs e)
            ((Button)sender).Content = "Click Me";

We’ll see next time that there’s a much easier way to do this, using a trigger.


About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

2 Responses to #665 – Reacting to MouseEnter / MouseLeave Events

  1. Pingback: Dew Drop – October 10, 2012 (#1,419) | Alvin Ashcraft's Morning Dew

  2. Pingback: #666 – Using a Trigger to React to the Mouse Being Over a Control « 2,000 Things You Should Know About WPF

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: