Question

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!!

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top