在我正在处理的WPF应用中,有一个Button
Style
定义了它自己的Template
(ControlTemplate
)。< / p>
我通过ControlTemplate.Triggers
处理视觉更新,如IsMouseOver
:
<Trigger Property="IsMouseOver" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="IsMouseOverBorder"
Storyboard.TargetProperty="Opacity"
To="1"
Duration="0:0:0.08" />
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="IsMouseOverBorder"
Storyboard.TargetProperty="Opacity"
To="0"
Duration="0:0:0.16" />
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
我已经为使用此风格的某些Button
添加了一个上下文菜单。完成此操作后,右键单击Button
时,上下文菜单会显示,但IsMouseOver
触发器会立即执行退出操作。我希望在上下文菜单打开时保持IsMouseOver
视觉状态 - 它看起来会更好。
我该怎么做?
这里有一些额外的触发器,我认为可以达到我想要的效果,但它并没有。第一个触发器(ContextMenuOpening
)有效,但第二个触发器没有 - 按钮处于永久IsMouseOver
外观状态。
<EventTrigger RoutedEvent="ContextMenuService.ContextMenuOpening">
<BeginStoryboard Name="ContextMenuOpeningStoryboard">
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="IsMouseOverBorder"
Storyboard.TargetProperty="Opacity"
To="1"
Duration="0" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="ContextMenuService.ContextMenuClosing">
<RemoveStoryboard BeginStoryboardName="ContextMenuOpeningStoryboard" />
</EventTrigger>
知道为什么这不起作用,或者另一种方法可行吗?
答案 0 :(得分:0)
我使用我的问题中定义的样式来使用它,但我必须手动引发ContextMenuService.ContextMenuClosingEvent
事件(使用一些反射来执行此操作):
<ContextMenu Closed="ContextMenu_Closed">...</ContextMenu>
代码背后:
private void ContextMenu_Closed(object sender, RoutedEventArgs args)
{
var type = typeof(ContextMenuEventArgs);
var contextMenuEventArgs = (ContextMenuEventArgs) type.Assembly.CreateInstance
(
type.FullName, false, BindingFlags.Instance | BindingFlags.NonPublic, null,
new object[] { this, false }, null, null
);
if (contextMenuEventArgs != null)
{
contextMenuEventArgs.RoutedEvent = ContextMenuService.ContextMenuClosingEvent;
_myToggleButton.RaiseEvent(contextMenuEventArgs);
}
}