context menu placement error

Eduardo Gomez 3,651 Reputation points
2023-02-05T22:30:32.07+00:00

So, I have my Listview, with a context menu.

The context menu only appears f the selected item is null

The problem that I have is that the context menu will appear at the same position as where I left the mouse

For example

User's image

User's image

I want my context menu to appear in the item

like this

User's image

<ListView x:Name="Explorer"
                  Height="350"
                  Margin="20"
                  ItemsSource="{Binding FilesCollection}"
                  ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                  SelectedItem="{Binding SelectedFile}">
            <ListView.InputBindings>
                <MouseBinding Command="{Binding OpenFileCommand}"
                              CommandParameter="{Binding SelectedFile}"
                              MouseAction="LeftDoubleClick" />
            </ListView.InputBindings>
            <Behaviors:Interaction.Triggers>
                <Behaviors:EventTrigger EventName="SelectionChanged">
                    <Behaviors:InvokeCommandAction Command="{Binding ItemChangedCommand}"
                                                   CommandParameter="{Binding SelectedFile}" />
                </Behaviors:EventTrigger>
            </Behaviors:Interaction.Triggers>
            <ListView.ContextMenu>
                <ContextMenu Visibility="{Binding IsMenuOpen}"
                             PlacementTarget="{Binding ElementName=Explorer}">
                    <MenuItem
                        Command="{Binding ShareCommand}"
                        CommandParameter="{Binding SelectedFile}"
                        FocusVisualStyle="{x:Null}"
                        FontFamily="{StaticResource mat}"
                        Header="{x:Static res:Lang.Share}"
                        Icon="{x:Static fonts:IconFont.ShareVariantOutline}" />

                    <MenuItem
                        Command="{Binding RenameCommand}"
                        CommandParameter="{Binding SelectedFile}"
                        FocusVisualStyle="{x:Null}"
                        FontFamily="{StaticResource mat}"
                        Header="{x:Static res:Lang.Rename}"
                        Icon="{x:Static fonts:IconFont.RenameBox}" />

                    <MenuItem
                        Command="{Binding DeleteCommand}"
                        CommandParameter="{Binding SelectedFile}"
                        FocusVisualStyle="{x:Null}"
                        FontFamily="{StaticResource mat}"
                        Header="{x:Static res:Lang.Delete}"
                        Icon="{x:Static fonts:IconFont.TrashCan}" />

                    <MenuItem
                        Command="{Binding ReadCommmand}"
                        CommandParameter="{Binding SelectedFile}"
                        FocusVisualStyle="{x:Null}"
                        FontFamily="{StaticResource mat}"
                        Header="{x:Static res:Lang.Read}"
                        Icon="{x:Static fonts:IconFont.PlayBox}"
                        Visibility="{Binding IsReadVisible}" />
                </ContextMenu>


In the view Model I trigger this

        private void OpenMenuAction(FileItem obj) {
            if (obj != null) {
                IsMenuOpen = Visibility.Visible;
            }
        }
Developer technologies | Windows Presentation Foundation
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.