Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event for MouseOver action in WPF

Tags:

wpf

I want to handle mouse over and mouse out events for a grid. Does WPF have events for this. Note: I dont want to use IsMouseOver property in my style. i have used MouseEnter and MouseLeave method but without much success.

like image 713
munna Avatar asked Sep 17 '10 09:09

munna


4 Answers

A WPF Grid control supports both the MouseEnter and MouseLeave events. You should be able to hook up event handlers for both.

like image 146
Eric Olsson Avatar answered Oct 20 '22 05:10

Eric Olsson


You can use EventTriggers to capture MouseEnter and MouseLeave events in XAML.

Here is a simple example that changes the background of a StackPanel in a Grid:

<Grid>
  <Grid.RowDefinitions>
    <RowDefinition/>
    <RowDefinition/>
  </Grid.RowDefinitions>
  <StackPanel Grid.Row="1" Background="Blue">
    <StackPanel.Style>
      <Style>
        <Style.Triggers>
          <EventTrigger RoutedEvent="StackPanel.MouseEnter">
            <EventTrigger.Actions>
              <BeginStoryboard>
                <Storyboard>
                  <ColorAnimation 
                      AutoReverse="False" 
                      Duration="0:0:1" 
                      From="Blue" To="Red"
                      AccelerationRatio="1" 
                      Storyboard.TargetProperty="(StackPanel.Background).(SolidColorBrush.Color)"
                      FillBehavior="HoldEnd">
                  </ColorAnimation>
                </Storyboard>
              </BeginStoryboard>
            </EventTrigger.Actions>
          </EventTrigger>
          <EventTrigger RoutedEvent="StackPanel.MouseLeave">
            <EventTrigger.Actions>
              <BeginStoryboard>
                <Storyboard>
                  <ColorAnimation 
                      AutoReverse="False" 
                      Duration="0:0:1" 
                      From="Red" To="Blue"
                      AccelerationRatio="1" 
                      Storyboard.TargetProperty="(StackPanel.Background).(SolidColorBrush.Color)"
                      FillBehavior="HoldEnd">
                  </ColorAnimation>
                </Storyboard>
              </BeginStoryboard>
            </EventTrigger.Actions>
          </EventTrigger>
        </Style.Triggers>
      </Style>
    </StackPanel.Style>
  </StackPanel>
</Grid>
like image 28
Zamboni Avatar answered Oct 20 '22 06:10

Zamboni


More simple : You can implement the two events PointerMoved and PointerExited. It worked for me.

like image 2
Myosotis Avatar answered Oct 20 '22 06:10

Myosotis


MouseEnter and MouseLeave events may be handled , you can check your code set e.handled = false;

like image 2
Park Wu Avatar answered Oct 20 '22 07:10

Park Wu