這裏是我的代碼:在WPF中,爲什麼MouseLeave會觸發而不是MouseDown?
<StackPanel>
<StackPanel.Resources>
<Style x:Key="stlNavButtonBorder" TargetType="Border">
<Setter Property="BorderBrush" Value="#570000FF" />
<Setter Property="BorderThickness" Value="5" />
<Setter Property="Height" Value="100" />
<Setter Property="Width" Value="200" />
<Setter Property="Margin" Value="10" />
<Style.Triggers>
<EventTrigger RoutedEvent="MouseEnter">
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetProperty="BorderBrush.Color"
To="blue"
Duration="0:0:0.25"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="MouseLeave">
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetProperty="BorderBrush.Color"
To="#570000FF"
Duration="0:0:0.25" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="MouseDown">
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetProperty="BorderBrush.Color"
To="Black"
Duration="0:0:0.25" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
<Style x:Key="stlNavButtonRectangle" TargetType="Rectangle">
<Setter Property="Fill" Value="#570000FF"/>
</Style>
<Style TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Style="{StaticResource stlNavButtonBorder}">
<Grid>
<Rectangle Style="{StaticResource stlNavButtonRectangle}"/>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</StackPanel.Resources>
<Button Content="Button 1" />
<Button Content="Button 2"/>
<Button Content="Button 3" />
</StackPanel>
它產生這些按鈕:
問題是,當鼠標停在按鈕上時,邊框不會從藍色變爲黑色,因爲我試圖在MouseDown
事件觸發器,而是消失,這是事件觸發器的MouseLeave
。
如何解決?謝謝。
請參閱本 - https://social.msdn.microsoft.com/Forums/vstudio/en-US/1069c828-bf5b-4777-a3ab-30e39369a83f/mousedown-event-also-triggers-mouseleave-and -mouseenter-events?forum = wpf – PaulF
如果您檢查默認控件模板,您會看到一些使用可視狀態組(過渡動畫),而另一些使用屬性上的觸發器,如「IsMouseOver」,「IsEnabled」和「IsPressed '。採用這些方法之一,您可能會獲得更多成功。 –
@PaulF - 我無法弄清楚這個鏈接的解決方案... – Sipo