#665 – Reacting to MouseEnter / MouseLeave Events
October 10, 2012 2 Comments
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"/> </StackPanel>
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.