是否可以在WPF中将样式触发器添加到Button以确定ContextMenu是否已打开?
如果是这样,如果打开了上下文菜单,我想为相应按钮的背景着色。
无法找到常规方法...
答案 0 :(得分:1)
这是一个解决方案:
<Grid ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Button Grid.Column="1" Margin="5" Content="Button" x:Name="theButton">
<Button.Background>
<SolidColorBrush x:Name="BackgroundBrush" Color="LightGray" />
</Button.Background>
<Button.ContextMenu>
<ContextMenu x:Name="contextMenu">
<MenuItem Header="Option 1" />
<MenuItem Header="Option 2" />
</ContextMenu>
</Button.ContextMenu>
<Button.Triggers>
<!-- This changed the color to red when the context menu is openning -->
<EventTrigger RoutedEvent="Button.ContextMenuOpening">
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="BackgroundBrush"
Storyboard.TargetProperty="Color" Duration="0:0:0" To="Red" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<!-- This changed the color back to light gray when the context menu is closing -->
<EventTrigger RoutedEvent="Button.ContextMenuClosing">
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="BackgroundBrush"
Storyboard.TargetProperty="Color" Duration="0:0:0" To="LightGray" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
</Grid>