Dan
Dan

Reputation: 163

Context Menu Command not Working

I'm working on a Windows Phone 8 app and I have some problems with a ContextMenu of a List Box. I use the toolkit ContextMenu in the ListBoxItemTemplate as follow:

<DataTemplate x:Key="ListBoxItemTemplate">
        <Grid Height="50" Background="#11414141"
            Margin="0,1,0,1">
            <toolkit:ContextMenuService.ContextMenu>
                <toolkit:ContextMenu>
                    <toolkit:MenuItem Header="Remove Transaction" Command="{Binding DataContext.RemoveTappedElementCommand}" CommandParameter="{Binding}"/>
                </toolkit:ContextMenu>
            </toolkit:ContextMenuService.ContextMenu>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="10"/>
                <ColumnDefinition Width="100"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="180"/>
            </Grid.ColumnDefinitions>
            <!--<StackPanel Grid.Column="0" Background="{Binding IsIncome, Converter={StaticResource TypeToColorConverter}}"/>-->
            <StackPanel Grid.Column="0" Background="#FFFB7C26"/>
            <TextBlock Grid.Column="1"
                Text="{Binding Date, StringFormat=dd/MM}"
                VerticalAlignment="Center"
                Margin="6,0,6,0"/>
            <TextBlock Grid.Column="2"
                Text="{Binding Capitolo}" 
                TextTrimming="WordEllipsis" 
                VerticalAlignment="Center"/>
            <Grid Grid.Column="3">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="15"/>
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding Amount, StringFormat=C}"
                    Grid.Column="0"
                    Foreground="{Binding IsIncome, Converter={StaticResource TypeToColorConverter}}"
                    VerticalAlignment="Center"
                    HorizontalAlignment="Right"
                    FontSize="25"
                    FontWeight="SemiBold"
                    Margin="4,0,4,0"/>
                <TextBlock Text="{Binding IsIncome, Converter={StaticResource TypeToSignStringConverter}}"
                    Grid.Column="1"
                    Foreground="{Binding IsIncome, Converter={StaticResource TypeToColorConverter}}"
                    VerticalAlignment="Center"                               
                    HorizontalAlignment="Center"
                    FontSize="25"
                    FontWeight="SemiBold"/>
            </Grid>
        </Grid>
    </DataTemplate>

I'm using MVVM pattern, so I tried to bind Command propery of the MenuItem to an ICommand of the ViewModel. But when I click on the MenuItem of the context menu, nothing happens, the command is not fired and I can't figure out the reason. Any help will be very appreciated.

Upvotes: 1

Views: 2543

Answers (1)

lisp
lisp

Reputation: 4198

There is a problem in your binding:

Command="{Binding DataContext.RemoveTappedElementCommand}"

This will not look for the RemoveTappedElementCommand on the top-level DataContext, but will instead look for the property DataContext on your item view model. To realy bind to the DataContext of the Page/Control contatining the ListBox, give that item a name

x:Name="Root"

and expand your binding:

Command="{Binding ElementName=Root, Path=DataContext.RemoveTappedElementCommand}"

Upvotes: 3

Related Questions