本文介绍了WPF 中鼠标悬停的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想处理网格的鼠标悬停和鼠标移出事件.WPF 是否有这方面的活动.注意:我不想在我的风格中使用 IsMouseOver 属性.我使用过 MouseEnter 和 MouseLeave 方法,但效果不佳.

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.

推荐答案

您可以使用 EventTriggers 在 XAML 中捕获 MouseEnter 和 MouseLeave 事件.

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

这是一个简单的例子,它改变了网格中 StackPanel 的背景:

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>

这篇关于WPF 中鼠标悬停的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 01:30