开发者

Event for MouseOver action in WPF

开发者 https://www.devze.com 2023-01-16 07:42 出处:网络
I want to handle mouse over and mouse out events f开发者_高级运维or a grid. Does WPF have events for this.

I want to handle mouse over and mouse out events f开发者_高级运维or 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.


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>


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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消