Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. How to enable Context menu for a listbox item.

How to enable Context menu for a listbox item.

Scheduled Pinned Locked Moved WPF
helptutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Aslesh
    wrote on last edited by
    #1

    public partial class Window1 : Window { public Window1() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { lstItems.Items.Add(new MyPeopleData { FirstName = "Han", LastName = "Solo", Age = 45, FavoriteMovie = "Star Wars" }); lstItems.Items.Add(new MyPeopleData { FirstName = "James", LastName = "Kirk", Age = 36, FavoriteMovie = "Star Trek" }); lstItems.Items.Add(new MyPeopleData { FirstName = "Martha", LastName = "Jones", Age = 24, FavoriteMovie = "Dr. Who" }); lstItems.Items.Add(new MyPeopleData { FirstName = "Will", LastName = "Smith", Age = 32, FavoriteMovie = "Independance Day" }); lstItems.Items.Add(new MyPeopleData { FirstName = "Christian", LastName = "Bale", Age = 40, FavoriteMovie = "The Dark Knight" }); lstItems.Items.Add(new MyPeopleData { FirstName = "Hugh", LastName = "Jackman", Age = 46, FavoriteMovie = "Wolverine" }); } } public class MyPeopleData { public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } public string FavoriteMovie { get; set; } } I want to enable context menu for the LSITBOXITEM. CAn any one help me how can i do this... Santhapur

    P 1 Reply Last reply
    0
    • A Aslesh

      public partial class Window1 : Window { public Window1() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { lstItems.Items.Add(new MyPeopleData { FirstName = "Han", LastName = "Solo", Age = 45, FavoriteMovie = "Star Wars" }); lstItems.Items.Add(new MyPeopleData { FirstName = "James", LastName = "Kirk", Age = 36, FavoriteMovie = "Star Trek" }); lstItems.Items.Add(new MyPeopleData { FirstName = "Martha", LastName = "Jones", Age = 24, FavoriteMovie = "Dr. Who" }); lstItems.Items.Add(new MyPeopleData { FirstName = "Will", LastName = "Smith", Age = 32, FavoriteMovie = "Independance Day" }); lstItems.Items.Add(new MyPeopleData { FirstName = "Christian", LastName = "Bale", Age = 40, FavoriteMovie = "The Dark Knight" }); lstItems.Items.Add(new MyPeopleData { FirstName = "Hugh", LastName = "Jackman", Age = 46, FavoriteMovie = "Wolverine" }); } } public class MyPeopleData { public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } public string FavoriteMovie { get; set; } } I want to enable context menu for the LSITBOXITEM. CAn any one help me how can i do this... Santhapur

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Santhapur, try this:

      <Window x:Class="HDI_WPF_ListItemTemplate_cs.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
      <Window.Resources>
      <ContextMenu x:Key="myMenu">
      <MenuItem Header="my text" />
      </ContextMenu>
      </Window.Resources>
      <Grid>
      <ListBox x:Name="lstItems" Width="300" MaxHeight="300" FontSize="16">
      <ListBox.ItemTemplate>
      <DataTemplate>
      <Border BorderBrush="Blue" Margin="0,4,0,4" BorderThickness="1" CornerRadius="5">
      <StackPanel Orientation="Vertical" ContextMenu="{StaticResource myMenu}">
      <StackPanel Orientation="Horizontal" Background="AntiqueWhite">
      <TextBlock FontSize="16" Text="{Binding Path=FirstName}" />
      <TextBlock FontSize="16" Text=" " />
      <TextBlock FontSize="16" Text="{Binding Path=LastName}" />
      </StackPanel>
      <TextBlock FontSize="12" Text="{Binding Path=Age}" />
      <TextBlock FontSize="12" Text="{Binding Path=FavoriteMovie}" />
      </StackPanel>
      </Border>
      </DataTemplate>
      </ListBox.ItemTemplate>
      <ListBox.ItemsPanel>
      <ItemsPanelTemplate>
      <StackPanel Orientation="Vertical" />
      </ItemsPanelTemplate>
      </ListBox.ItemsPanel>
      </ListBox>
      </Grid>
      </Window>

      Here I've added the context menu as a resource, and referred to it from the StackPanel. By doing this, the context menu will only appear when you click on the item itself rather than space at the side.

      Deja View - the feeling that you've seen this post before.

      My blog | My articles

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups