1

I have a listbox, For each list box item I need to display a context menu item based on the data it is bound to. here is my listbox

<ListBox x:Name="pdflist" ItemsSource="{Binding}" Margin="18,0,7,0" SelectionChanged="pdflist_SelectionChanged"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <toolkit:ContextMenuService.ContextMenu> <toolkit:ContextMenu x:Name="mymenu" ItemsSource={Binding}> <toolkit:ContextMenu.ItemTemplate> <DataTemplate> <toolkit:MenuItem Header="{Binding isFavorite}" Click="favorite_Click" /> </DataTemplate> </toolkit:ContextMenu.ItemTemplate> </toolkit:ContextMenu> </toolkit:ContextMenuService.ContextMenu> <Grid Width="420"> <Grid.ColumnDefinitions> <ColumnDefinition Width="30"></ColumnDefinition> <ColumnDefinition Width="350"></ColumnDefinition> <ColumnDefinition Width="60"></ColumnDefinition> </Grid.ColumnDefinitions> <Image VerticalAlignment="Top" Margin="0,20,0,0" Height="20" Width="25" Source="/Assets/PDF.png" Grid.Column="0" Stretch="None" > </Image> <TextBlock TextWrapping="Wrap" Grid.Column="1" Foreground="Black" FontSize="30" Text="{Binding name}"></TextBlock> <Image Height="20" Width="25" Grid.Column="2" Source="{Binding isFavorite,Converter={StaticResource typeconvert}}"></Image> </Grid> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> 

The data bound to list box has the model

 public class resources { public string name { get; set; } public bool isRead { get; set; } public bool isFavorite { get; set; } } 

When I run my code,I am unable to view any menu items in context menu..

I have tried this

<ItemsControl ItemsSource="{Binding isFavorite}" Tag="{Binding ElementName=pdflist, Path=DataContext}"> <toolkit:ContextMenuService.ContextMenu> <toolkit:ContextMenu> <toolkit:MenuItem Header="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}" Click="favorite_Click"/> </toolkit:ContextMenu> </toolkit:ContextMenuService.ContextMenu> </ItemsControl> 

On long press, context menu itself doesnt appear!!

8
  • What is the type of each item in your ListBox? Is is resources? Is ListBox.ItemsSource some sort of list of resources? Commented Apr 5, 2014 at 10:44
  • Its a list<resources> List<resources> retrievedpdf = "some query". pdflist.ItemsSource = retrievedpdf; Commented Apr 5, 2014 at 10:46
  • The property ItemsSource in ContextMenu is not correctly written. Commented Apr 5, 2014 at 10:56
  • @Roman I have edited it, Still no luck.. Commented Apr 5, 2014 at 11:06
  • 1
    because ItemsSource expects IEnumerable and resources is just one item, not a list Commented Apr 5, 2014 at 11:26

2 Answers 2

1
+50

What you might want to do is:

<DataTemplate> <StackPanel> <toolkit:ContextMenuService.ContextMenu> <toolkit:ContextMenu> <toolkit:MenuItem Header="add to favourites" Visibility="{Binding isFavorite, Converter={StaticResource BoolToVisibility}}" Tap="HandleFavouriteTap"/> <toolkit:MenuItem Header="remove from favourites" Visibility="{Binding isFavorite, Converter={StaticResource BoolToCollapsed}}" Tap="HandleFavouriteTap"/> </toolkit:ContextMenu> </toolkit:ContextMenuService.ContextMenu> <Grid Width="420"> ... </Grid> </StackPanel> </DataTemplate> 

You should not use the ItemsSource property, because you don't have a IEnumerable property on your View Model (resources class) that could be used as a list of available commands. You just want one command, but different depending on a value of isFavorite property - so add two MenuItems and bind their Visibility. In the above solution you will need two bool-to-visilibity converters defined as recourcesn.

Note, that in my experience it is best to avoid ItemsSource of the ContextMenu altogether. Because then you have to define DataTemplate and you place MenuItem inside the template. As a result MenuItem of the DataTemplate is wrapped with another MenuItem. You may not notice anything strange when using only Tap event, but when binding to Command the MenuItems will not behave as expected.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the answer,If i dont assign itemsSource for context menu, Will the itemsSource be taken from its parent say listbox?
@PrasannaAarthi No. Setting the ItemsSource means that you want to automatically populate the ItemsPanel with the contents of the collection you're binding to. You don't have such collection on your ViewModel. However, DataContext of the ContextMenu is inherited, so if your Item ViewModel (your resources class) has Commands, you can bind in MenuItems to those Commands.
I tried your code, this is my convertor public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value == "true") { return Visibility.Visible; } else { return Visibility.Collapsed; } } But then menu is blank, though visible is returned by convertor.. any clues?
Thank you, Had to make some modification in my convertor.. It works!!
1

ContextMenu is not part of the VisualTree of your UserControl, so Binding won't work out of the Box.

Please take a loot at this post. This is a nice and comfortable workaround for this issue.

1 Comment

I have made an edit in my code, using your solution, now context menu doesnt even appear..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.