右键单击上下文菜单未触发

时间:2013-11-29 22:31:32

标签: c# wpf mvvm

我有以下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>

我的问题是如何在右键单击时触发上下文菜单,并在左键单击时打开命令?

感谢您的时间。

1 个答案:

答案 0 :(得分:1)

如果您只想在单击鼠标左键时运行“加载选定文件”命令,则应使用UIElement.MouseLeftButtonDown Event代替。