我想处理鼠标移过和鼠标移出事件的网格。 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/