我有以下ListBox
。这很好用,当我左键单击TextBlock
时,它将打开最近的文件。但是,当我右键单击时,上下文菜单不会打开,而是像单击鼠标左键一样,并且会触发LoadSelectedFileCommand
命令,从而打开最近的文档。以下是ListBox
XAML:
<ListBox ItemsSource="{Binding RecentFiles, NotifyOnSourceUpdated=True, IsAsync=True, Mode=TwoWay}"
ItemContainerStyle="{StaticResource MenuListBoxItem}"
VerticalAlignment="Stretch"
Grid.Row="6" >
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type RecentObjects:RecentFile}">
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsPinned}"
Style="{StaticResource imageCheckBox}"
ToolTip="{Binding IsPinned, Converter={StaticResource BooleanToVariableStringConverter}}"
DataAccess:DocumentCheckBox.IsCheckedOnData="{DynamicResource Pinned}"
DataAccess:DocumentCheckBox.IsCheckedOffData="{DynamicResource UnPinned}"
AttachedCommand:CommandBehavior.Event="Click"
AttachedCommand:CommandBehavior.Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MahAppsControls:MetroContentControl}}, Path=DataContext.UpdateRecentFilesCommand}" >
</CheckBox>
<TextBlock Text="{Binding FileName}"
Style="{StaticResource MenuTextBlock}"
ToolTip="{Binding FullFileName}"
AttachedCommand:CommandBehavior.Event="MouseDown"
AttachedCommand:CommandBehavior.Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MahAppsControls:MetroContentControl}}, Path=DataContext.LoadSelectedFileCommand}"
AttachedCommand:CommandBehavior.CommandParameter="{Binding FullFileName}">
<TextBlock.ContextMenu>
<ContextMenu>
<MenuItem Header="Open Study" />
<MenuItem Header="Open Containing Folder" />
<MenuItem Header="Remove From List" />
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我的问题是,如何使右键单击时触发上下文菜单,而单击左键时打开命令?
谢谢你的时间。
最佳答案
如果只希望在单击鼠标左键时运行LoadSelectedFileCommand,则应改为使用UIElement.MouseLeftButtonDown Event。
关于c# - 右键单击时不触发上下文菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20294210/