你可以通过几种方式做到这一点,这里有两种:
1) 纯 XAML
<Button Content="My Button">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="ContextMenu.IsOpen">
<DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="True"/>
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Button.Triggers>
<Button.ContextMenu>
<ContextMenu >
<MenuItem Header="ABC"/>
</ContextMenu>
</Button.ContextMenu>
</Button>
2)使用事件
<Button Content="My Button" Click="Button_Click">
<Button.ContextMenu>
<ContextMenu >
<MenuItem Header="ABC"/>
</ContextMenu>
</Button.ContextMenu>
</Button>
private void Button_Click(object sender, RoutedEventArgs e)
{
ContextMenu contextMenu = (sender as Button).ContextMenu;
contextMenu.PlacementTarget = sender as UIElement;
contextMenu.IsOpen = true;
}