簡體   English   中英

上下文菜單ICommand無法在我的mvvm Windows Phone應用程序中觸發

[英]Context menu ICommand doesn't fire in my mvvm windows phone app

我在視圖中定義了以下上下文菜單。

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

我將下面的ViewModel設置為上面的視圖的數據上下文。當我使用單擊事件的命令綁定時,ICommand適用於按鈕等控件。 但這不適用於我的contextmenu命令。

  public System.Windows.Input.ICommand ViewTrackCommand
    {
        get
        {

            return new DelegateCommand((o) =>
            {

                Task.Factory.StartNew(() =>
                {
                    PlayTrack();
                });
            });
        }
   }


 public System.Windows.Input.ICommand DeleteTrackCommand
    {
        get
        {

            return new DelegateCommand((o) =>
            {

                Task.Factory.StartNew(() =>
                {
                    DeleteTrack();
                });
            });
        }
   }

我為按鈕單擊事件嘗試了類似的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

您需要像這樣指出清晰的ViewModel的Path和Source:

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

希望這對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM