我对ContextMenu有一个小问题。
<Window.Resources>
<Style x:Key="ButtonWithContextMenuStyle" TargetType="Button">
<Setter Property="Background" Value="#cbeb00" />
<Setter Property="Foreground" Value="#505050" />
<Setter Property="FontSize" Value="12" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border CornerRadius="4" Background="{TemplateBinding Background}"
BorderThickness="0">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Margin="{TemplateBinding Padding}" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#aaaaaa"/>
<Setter Property="Foreground" Value="#dcff00"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Command="{Binding EditUserCommand}" Header="{DynamicResource EditUserContextMenuMW}"/>
<MenuItem Command="{Binding OpenHistoryCommand}" Header="{DynamicResource ConversationHistoryContextMenuMW}"/>
<MenuItem Command="{Binding OpenHistoryCommand}" Header="{DynamicResource BuyCreditContextMenuMW}"/>
<Separator />
<MenuItem Command="{Binding LogoutCommand}" Header="{DynamicResource LogoutContextMenuMW}"/>
</ContextMenu>
</Setter.Value>
</Setter>
<Style.Triggers>
<EventTrigger RoutedEvent="Click" >
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="ContextMenu.IsOpen">
<DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="True"/>
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
当我单击鼠标右键时,一切正常。 ContextMenu打开,我可以在MenuItem上调用Command。
当我点击鼠标左键时,我想看到相同的行为。但是,只有ContextMenu打开,我无法调用Command。
有人能告诉我我做错了什么吗?
答案 0 :(得分:0)
<Window x:Class="ContextMenuMouseDownStack.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel Margin="10">
<StackPanel.Resources>
<Style TargetType="Button">
<Style.Triggers>
<EventTrigger RoutedEvent="Click">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="ContextMenu.IsOpen">
<DiscreteBooleanKeyFrame KeyTime="0:0:2" Value="True"/>
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
</Style>
</StackPanel.Resources>
<Button Content="Click!" VerticalAlignment="Center" HorizontalAlignment="Center">
<Button.ContextMenu>
<ContextMenu>
<MenuItem Header="Menu item 1" Click="MenuItem_Click"/>
<MenuItem Header="Menu item 2" />
<Separator />
<MenuItem Header="Menu item 3" />
</ContextMenu>
</Button.ContextMenu>
</Button>
</StackPanel>
在这个类似于你的例子中,Click事件可以完成工作。实际上,我没有那里的Command,只是处理程序背后的简单代码。它以两种方式到达它,鼠标右键或左键单击。只是为了测试,你可以尝试一个简单的处理程序吗?
更新1:
对我来说,它也适用于两种方式:
<EventTrigger RoutedEvent="PreviewMouseDown">
更新2
你的故事板似乎毁了这些东西:
我用这样的Popup改变了这个:
<Popup x:Name="btnMenuPopup" Placement="Mouse" StaysOpen="False">
<Border BorderBrush="Black" BorderThickness="2" CornerRadius="2">
<Menu>
<MenuItem Header="Menu item 1" Command="{Binding EditCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/>
<MenuItem Header="Menu item 2" />
<Separator />
<MenuItem Header="Menu item 3" />
<Menu.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</Menu.ItemsPanel>
</Menu>
</Border>
</Popup>
为了快速,我在按钮上添加了一个点击事件,这将打开弹出窗口。
private void Button_Click(object sender, RoutedEventArgs e)
{
btnMenuPopup.IsOpen = true;
}
现在应该可以了。
这是弹出窗口的命令:
Command="{Binding CommandShowPopup}" CommandParameter="{Binding ElementName=btnMenuPopup}"
命令实施:
public class CommandShowPopup : ICommand
{
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
(parameter as Popup).IsOpen = true;
}
}
答案 1 :(得分:-2)
您需要使用MouseRightButtonDown
路由事件。
<EventTrigger RoutedEvent="MouseRightButtonDown">