wpf - WPF中MouseOver Action 的事件

标签 wpf

我想处理鼠标移过和鼠标移出事件的网格。 WPF是否有与此相关的事件。
注意:我不想在我的样式中使用IsMouseOver属性。
我使用过MouseEnter和MouseLeave方法,但没有取得太大的成功。

最佳答案

您可以使用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>

关于wpf - WPF中MouseOver Action 的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3734061/

相关文章:

c# - 如何为要设置动画的属性上的数据更改触发的元素设置动画

wpf - 我怎样才能知道哪个TextBox最后一次获得焦点?

wpf - DataContext 未绑定(bind)在 Style.Trigger 中

c# - 什么时候为 WPF/MVVM 使用事件和命令?

c# - 如何将 YUV (444) 转换为 WPF 上的显示,更喜欢以某种方式使用 DirectX 进行硬件加速

wpf - DispatcherTimer 在 WPF 应用程序中未触发

c# - 由于未注册 Windows,Windows 10 分配的访问权限应用无法启动。启动契约(Contract)

wpf - MVVM的WPF ProgressBar可见性不起作用

.net - WPF - 在 SYSTEM 帐户下执行进程时性能低下

WPF MVVM 动态子菜单绑定(bind)问题