WPF中MouseOver操作的事件

时间:2010-09-17 09:20:55

标签: wpf

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

4 个答案:

答案 0 :(得分:7)

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

这是一个简单的例子,用于更改网格中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>

答案 1 :(得分:7)

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

答案 2 :(得分:2)

可以处理MouseEnter和MouseLeave事件,你可以检查你的代码集e.handled = flase;

答案 3 :(得分:2)

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