上下文菜单命令不起作用

时间:2014-10-11 12:59:56

标签: xaml windows-phone-8 mvvm command contextmenu

我正在使用Windows Phone 8应用,但我遇到ContextMenu List Box的问题。我使用ContextMenu中的工具包ListBoxItemTemplate,如下所示:

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

我使用MVVM模式,因此我尝试将MenuItem的Command propery绑定到ViewModel的ICommand。但是,当我点击上下文菜单的MenuItem时,没有任何反应,命令没有被触发,我无法找出原因。任何帮助将非常感激。

1 个答案:

答案 0 :(得分:1)

绑定存在问题:

Command="{Binding DataContext.RemoveTappedElementCommand}"

这将在顶级DataContext上查找RemoveTappedElementCommand,而是在项目视图模型上查找属性DataContext。 要重新绑定到包含ListBox的Page / Control的DataContext,请为该项指定名称

x:Name="Root"

并展开你的装订:

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