Heldenkrieger01
Heldenkrieger01

Reputation: 369

Context Menu in ListView, choosing an Item

I want to have a Context Menu when clicking an Item in my ListView

<ListView x:Name="resultsView" Grid.Column="1" Margin="10" Grid.Row="1" FontFamily="Verdana" FontSize="14">
        <ListView.ContextMenu>
            <ContextMenu FontFamily="Verdana" FontSize="14">
                <MenuItem Header="Open Folder" Click="openFolder_Click" FontFamily="Verdana" FontSize="14"/>
                <Separator/>
                <MenuItem Header="Copy Path" Click="copyPath_Click" FontFamily="Verdana" FontSize="14"/>
            </ContextMenu>
        </ListView.ContextMenu>

        <ListView.View>
            <GridView x:Name="gridView">
                <GridViewColumn x:Name="name" Header="Name" DisplayMemberBinding="{Binding Name}"/>
                <GridViewColumn x:Name="folder" Header="Folder" DisplayMemberBinding="{Binding Folder}"/>
                <GridViewColumn x:Name="location" Header="Path" DisplayMemberBinding="{Binding Location}"/>
            </GridView>
        </ListView.View>
</ListView>

Here is how I add Items to my ListView (results is a List of SearchFolder's):

results.Add(new SearchFolder { Name = Path.GetFileName(directory), Folder = Path.GetFileName(Path.GetDirectoryName(directory)), Location = directory });
resultsView.ItemsSource = results;

And my SearchFolder class

class SearchFolder
{
    public string Name { get; set; }

    public string Folder { get; set; }

    public string Location { get; set; }
}

How do I create a ContextMenu, where the action (copy Path or open Folder) is executed on the clicked item in the ListView?

/edit: this way, the ContextMenu shows up no matter where I right click. But I can't find a way to access the clicked item, this does NOT WORK:

MenuItem temp = sender as MenuItem;
        if (temp != null)
        {
            ContextMenu anotherTemp = temp.Parent as ContextMenu;
            if (anotherTemp != null)
            {
                ListViewItem clickedItem = anotherTemp.PlacementTarget as ListViewItem;
                if (clickedItem != null)
                {
                    Console.WriteLine("Copy Path " + clickedItem.Name);
                }
            }
        }

Upvotes: 1

Views: 2112

Answers (3)

D Ie
D Ie

Reputation: 841

Try this on your click event handler:

private void ItemRightClick(object sender, EventArgs e)
{
    MenuItem item = sender as MenuItem;
    if (item!= null)
    {
        ContextMenu contextMenu = item.Parent;
        Control clickedControl = contextMenu.SourceControl;
    }
}

haven't checked it...

anyways, another way of doing it is using the 'MouseDown' event and checking if the button clicked was the right one:

private void ItemMouseDown(object sender, MouseEventArgs e)
{
     if(e.Button == MouseButtons.Right)
     {
          ListViewItem item = listView.GetItemAt(e.X, e.Y);
          if(item != null)
          {
             // here we have
          }
     }
}

Upvotes: 0

Heldenkrieger01
Heldenkrieger01

Reputation: 369

I finally managed to do what I wanted, I left the context menu as it was before and was able to get the item with the following code in the Event Handlers for the MenuItems in the ContextMenu

if(resultsView.SelectedIndex>-1)
        {
            SearchFolder selectedItem;
            selectedItem = (SearchFolder)resultsView.SelectedItem;
            //do stuff with selectedItem
        }

Upvotes: 1

Harmi
Harmi

Reputation: 188

ListView have one event "SelectionChanged".Use this event you can set all your code in this event and access a item in sender.

Code:

<ListBox Grid.Row="1" Grid.Column="1" Margin="10,10,0,0" x:Name="lstBank" Style="{StaticResource TransparentListBox}" ItemTemplate="{StaticResource BankDataTemplate}"
                     MinHeight="35" MaxHeight="220" SelectionMode="Single" ItemsSource="{Binding Path=Banks}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" SelectionChanged="lstBank_SelectionChanged" />

and set code in class file.

 private void lstBank_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) {

        }

Upvotes: 3

Related Questions