How to enable Context menu for a listbox item.
-
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
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 PageJust a grain of sand on the worlds beaches.
-
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 PageJust a grain of sand on the worlds beaches.
-
Do you want your context menu in XAML or code?
Cheers, Karl
» CodeProject 2008 MVP My Blog | Mole's Home PageJust a grain of sand on the worlds beaches.
-
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
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.
modified on Tuesday, September 2, 2008 5:05 AM
-
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.
modified on Tuesday, September 2, 2008 5:05 AM
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
-
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
Santhapur - I can't see your code. Could you post it again using the
<
and>
tags instead?Deja View - the feeling that you've seen this post before.
-
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
-
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.
-
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.
-
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.
-
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.