WPF 中鼠标悬停的事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3734061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:07:52  来源:igfitidea点击:

Event for MouseOver action in WPF

wpf

提问by munna

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.

我想处理网格的鼠标悬停和鼠标移出事件。WPF 是否有这方面的活动。注意:我不想在我的风格中使用 IsMouseOver 属性。我使用过 MouseEnter 和 MouseLeave 方法,但没有取得多大成功。

回答by Zamboni

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

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

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

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

<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>

回答by Eric Olsson

A WPF Grid control supports both the MouseEnterand MouseLeaveevents. You should be able to hook up event handlers for both.

WPF 网格控件支持MouseEnterMouseLeave事件。您应该能够为两者连接事件处理程序。

回答by Park Wu

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

可能会处理 MouseEnter 和 MouseLeave 事件,您可以检查您的代码集 e.handled = false;

回答by Myosotis

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

更简单:您可以实现 PointerMoved 和 PointerExited 两个事件。它对我有用。