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
tutorialquestion
13 Posts 3 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.
  • L Lost User

    You can create your Context Menu in code or XAML and simply add it the ListBoxItem.ContextMenu property for each item. Another way would be to add a Context Menu for the ListBox and perform hit testing for which ListBoxItem you are over.

    Cheers, Karl » CodeProject 2008 MVP My Blog | Mole's Home Page

    Just a grain of sand on the worlds beaches.

    A Offline
    A Offline
    Aslesh
    wrote on last edited by
    #3

    IF uou have any code snippet that would be helpful for me...

    L 1 Reply Last reply
    0
    • A Aslesh

      IF uou have any code snippet that would be helpful for me...

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #4

      Do you want your context menu in XAML or code?

      Cheers, Karl » CodeProject 2008 MVP My Blog | Mole's Home Page

      Just a grain of sand on the worlds beaches.

      A 1 Reply Last reply
      0
      • L Lost User

        Do you want your context menu in XAML or code?

        Cheers, Karl » CodeProject 2008 MVP My Blog | Mole's Home Page

        Just a grain of sand on the worlds beaches.

        A Offline
        A Offline
        Aslesh
        wrote on last edited by
        #5

        either is fine with me... if you can send both thats too good

        1 Reply Last reply
        0
        • A Aslesh

          Hi, I will get a Set of results in a listbox. How can i enable a item by right clicking on it.. I would like to show a menu when user right clicks on the listbox item. Santhapur

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

          I hope that this helps:

          <Window
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns: d="http://schemas.microsoft.com/expression/blend/2006"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
          mc:Ignorable="d"
          x:Class="ListViewContextSample.Window1"
          x:Name="Window"
          Title="Window1"
          Width="640" Height="480">

          <Window.Resources>
          <XmlDataProvider x:Key="WPFRSSFeed" d:IsDataSource="True"
          Source="http://www.codeproject.com/WebServices/MessageRSS.aspx?fid=1004114"/>
          <DataTemplate x:Key="itemTemplate">
          <StackPanel>
          <TextBlock Text="{Binding Mode=OneWay, XPath=title}"/>
          <TextBlock Text="{Binding Mode=OneWay, XPath=description}"/>
          <TextBlock Text="{Binding Mode=OneWay, XPath=link}"/>
          <TextBlock Text="{Binding Mode=OneWay, XPath=author}"/>
          <TextBlock Text="{Binding Mode=OneWay, XPath=pubDate}"/>
          <TextBlock Text="{Binding Mode=OneWay, XPath=subject}"/>
          </StackPanel>
          </DataTemplate>
          </Window.Resources>

          <StackPanel x:Name="LayoutRoot">
          <ScrollViewer IsTabStop="True" >
          <ItemsControl ItemTemplate="{DynamicResource itemTemplate}"
          ItemsSource="{Binding Mode=Default, Source={StaticResource WPFRSSFeed},
          XPath=/rss/channel/item}">
          <ItemsControl.ContextMenu>
          <ContextMenu Name="MyContextMenu">
          <MenuItem Header="This is my menu" />
          </ContextMenu>
          </ItemsControl.ContextMenu>
          </ItemsControl>
          </ScrollViewer>
          </StackPanel>
          </Window>

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

          My blog | My articles

          modified on Tuesday, September 2, 2008 5:05 AM

          A 1 Reply Last reply
          0
          • P Pete OHanlon

            I hope that this helps:

            <Window
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns: d="http://schemas.microsoft.com/expression/blend/2006"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            mc:Ignorable="d"
            x:Class="ListViewContextSample.Window1"
            x:Name="Window"
            Title="Window1"
            Width="640" Height="480">

            <Window.Resources>
            <XmlDataProvider x:Key="WPFRSSFeed" d:IsDataSource="True"
            Source="http://www.codeproject.com/WebServices/MessageRSS.aspx?fid=1004114"/>
            <DataTemplate x:Key="itemTemplate">
            <StackPanel>
            <TextBlock Text="{Binding Mode=OneWay, XPath=title}"/>
            <TextBlock Text="{Binding Mode=OneWay, XPath=description}"/>
            <TextBlock Text="{Binding Mode=OneWay, XPath=link}"/>
            <TextBlock Text="{Binding Mode=OneWay, XPath=author}"/>
            <TextBlock Text="{Binding Mode=OneWay, XPath=pubDate}"/>
            <TextBlock Text="{Binding Mode=OneWay, XPath=subject}"/>
            </StackPanel>
            </DataTemplate>
            </Window.Resources>

            <StackPanel x:Name="LayoutRoot">
            <ScrollViewer IsTabStop="True" >
            <ItemsControl ItemTemplate="{DynamicResource itemTemplate}"
            ItemsSource="{Binding Mode=Default, Source={StaticResource WPFRSSFeed},
            XPath=/rss/channel/item}">
            <ItemsControl.ContextMenu>
            <ContextMenu Name="MyContextMenu">
            <MenuItem Header="This is my menu" />
            </ContextMenu>
            </ItemsControl.ContextMenu>
            </ItemsControl>
            </ScrollViewer>
            </StackPanel>
            </Window>

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

            My blog | My articles

            modified on Tuesday, September 2, 2008 5:05 AM

            A Offline
            A Offline
            Aslesh
            wrote on last edited by
            #7

            I have written like this.. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"> ListViewItem This enables Contextmenu for the listBox. But i want to enable Context menu only for list box items...Is it Possible? Santhapur

            P A 2 Replies Last reply
            0
            • A Aslesh

              I have written like this.. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"> ListViewItem This enables Contextmenu for the listBox. But i want to enable Context menu only for list box items...Is it Possible? Santhapur

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

              Santhapur - I can't see your code. Could you post it again using the &lt; and &gt; tags instead?

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

              My blog | My articles

              1 Reply Last reply
              0
              • A Aslesh

                I have written like this.. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"> ListViewItem This enables Contextmenu for the listBox. But i want to enable Context menu only for list box items...Is it Possible? Santhapur

                A Offline
                A Offline
                Aslesh
                wrote on last edited by
                #9

                Santhapur Santhapur

                P 1 Reply Last reply
                0
                • A Aslesh

                  Santhapur Santhapur

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

                  Santhapur - the easy way to do this is to blow the ListBoxItem open a bit. Consider the following bit that replaces the entry in the ListBox section.

                  <ListBoxItem>
                  <ListBoxItem.Content>
                  <TextBlock Text="Santhapur">
                  <TextBlock.ContextMenu>
                  <ContextMenu>
                  <MenuItem Header="Send" />
                  </ContextMenu>
                  </TextBlock.ContextMenu>
                  </TextBlock>
                  </ListBoxItem.Content>
                  </ListBoxItem>

                  That should do it for you. The context menu is now associated with the textblock, which is the list box item.

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

                  My blog | My articles

                  A 1 Reply Last reply
                  0
                  • P Pete OHanlon

                    Santhapur - the easy way to do this is to blow the ListBoxItem open a bit. Consider the following bit that replaces the entry in the ListBox section.

                    <ListBoxItem>
                    <ListBoxItem.Content>
                    <TextBlock Text="Santhapur">
                    <TextBlock.ContextMenu>
                    <ContextMenu>
                    <MenuItem Header="Send" />
                    </ContextMenu>
                    </TextBlock.ContextMenu>
                    </TextBlock>
                    </ListBoxItem.Content>
                    </ListBoxItem>

                    That should do it for you. The context menu is now associated with the textblock, which is the list box item.

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

                    My blog | My articles

                    A Offline
                    A Offline
                    Aslesh
                    wrote on last edited by
                    #11

                    Thanks Santhapur

                    P 1 Reply Last reply
                    0
                    • A Aslesh

                      Thanks Santhapur

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

                      You're welcome. Don't you just love WPF? So many ways you can do things - and easier to get at EXACTLY the thing you want to do, rather than having to fight your way through APIs.

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

                      My blog | My articles

                      A 1 Reply Last reply
                      0
                      • P Pete OHanlon

                        You're welcome. Don't you just love WPF? So many ways you can do things - and easier to get at EXACTLY the thing you want to do, rather than having to fight your way through APIs.

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

                        My blog | My articles

                        A Offline
                        A Offline
                        Aslesh
                        wrote on last edited by
                        #13

                        I am using I wrote context menu inside the Grid tags. But i am not getting the menu on it.. can any one help

                        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