How to enable Context menu for a listbox item.
-
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
-
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
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.