Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 上下文菜单ICommand不';t在我的mvvm windows phone应用程序中启动_C#_Windows Phone 7_Mvvm_Windows Phone 8_Contextmenu - Fatal编程技术网

C# 上下文菜单ICommand不';t在我的mvvm windows phone应用程序中启动

C# 上下文菜单ICommand不';t在我的mvvm windows phone应用程序中启动,c#,windows-phone-7,mvvm,windows-phone-8,contextmenu,C#,Windows Phone 7,Mvvm,Windows Phone 8,Contextmenu,我在视图中定义了以下上下文菜单 <ListBox x:Name="lstSavedTracks" ItemsSource="{Binding SavedMusicTracksDataSource}" Grid.Row="1" Margin="0,10,0,0" > <ListBox.ItemTemplate > <DataTemp

我在视图中定义了以下上下文菜单

     <ListBox x:Name="lstSavedTracks"   ItemsSource="{Binding SavedMusicTracksDataSource}"          Grid.Row="1" Margin="0,10,0,0"  >
                        <ListBox.ItemTemplate >
                            <DataTemplate >

                                <StackPanel >
                                    <toolkit:ContextMenuService.ContextMenu>
                                        <toolkit:ContextMenu>
                                            <toolkit:MenuItem Header="view" CommandParameter="{Binding}"   ItemsSource="{Binding Path=PlayTrackCommand}"/>
                                            <toolkit:MenuItem Header="delete" CommandParameter="{Binding}"  Command="{Binding Path=DeleteTrackCommand}"/>
                                        </toolkit:ContextMenu>
                                    </toolkit:ContextMenuService.ContextMenu>

                                    <TextBlock Foreground="White" FontSize="20"  Text="{Binding TrackTitle}"  TextWrapping="Wrap"></TextBlock>
                                    <Line MinHeight="5"></Line>

                                </StackPanel>
                            </DataTemplate>

                        </ListBox.ItemTemplate>
                    </ListBox>
我为按钮单击事件尝试了类似的icommand绑定,它们工作得很好。但是它在上下文菜单中不起作用。这里有我遗漏的东西吗

仅供参考:适用于按钮的Icommand实现

public class DelegateCommand : System.Windows.Input.ICommand
{
    private readonly Predicate<object> _canExecute;
    private readonly Action<object> _execute;

    public DelegateCommand(Action<object> execute)
        : this(execute, null)
    {
    }


    public DelegateCommand(Action<object> execute, Predicate<object> canExecute)
    {
        _execute = execute;
        _canExecute = canExecute;
    }

    public bool CanExecute(object parameter)
    {
        if (_canExecute == null)
        {
            return true;
        }

        return _canExecute(parameter);
    }


    public void Execute(object parameter)
    {
        _execute(parameter);
    }

    public event EventHandler CanExecuteChanged;
}//end of class
公共类DelegateCommand:System.Windows.Input.ICommand
{
私有只读谓词_canExecute;
私有只读操作\u执行;
公共DelegateCommand(操作执行)
:此(执行,空)
{
}
公共DelegateCommand(操作执行,谓词canExecute)
{
_执行=执行;
_canExecute=canExecute;
}
公共布尔CanExecute(对象参数)
{
如果(_canExecute==null)
{
返回true;
}
返回_canExecute(参数);
}
public void Execute(对象参数)
{
_执行(参数);
}
公共事件处理程序CanExecuteChanged;
}//下课

您需要指出ViewModel的清晰路径和来源,如下所示:

Command=“{Binding TestVM.DeleteTrackCommand,Source={StaticResource Locator}”


希望这对您有所帮助。

您是否在codeplex上查看了提供的示例?它们也可以使用ICommand,但在绑定命令时不使用“Path”属性。简单地说:Command=“{Binding DeleteTrackCommand}”我已经检查过了,但我不想使用后面的代码..这是我唯一无法应用mvvm的地方。。
public class DelegateCommand : System.Windows.Input.ICommand
{
    private readonly Predicate<object> _canExecute;
    private readonly Action<object> _execute;

    public DelegateCommand(Action<object> execute)
        : this(execute, null)
    {
    }


    public DelegateCommand(Action<object> execute, Predicate<object> canExecute)
    {
        _execute = execute;
        _canExecute = canExecute;
    }

    public bool CanExecute(object parameter)
    {
        if (_canExecute == null)
        {
            return true;
        }

        return _canExecute(parameter);
    }


    public void Execute(object parameter)
    {
        _execute(parameter);
    }

    public event EventHandler CanExecuteChanged;
}//end of class