C# CommandParameter在MVVM中如何工作?

C# CommandParameter在MVVM中如何工作?,c#,wpf,mvvm,C#,Wpf,Mvvm,我想在CommandProvider类中实现CommandParameter,它用于命令(按钮等)并从ICommand继承,但我不知道如何实现它。例如: XAML <TreeViewItem Header="Playlist" ItemsSource="{Binding ItemSourceTree}"> <i:Interaction.Triggers> <i:EventTrigger EventName="MouseDoubleClick">

我想在CommandProvider类中实现CommandParameter,它用于命令(按钮等)并从ICommand继承,但我不知道如何实现它。例如:

XAML

<TreeViewItem Header="Playlist" ItemsSource="{Binding ItemSourceTree}">
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseDoubleClick">
      <i:InvokeCommandAction Command="{Binding Path=NewPlaylist}"
                             CommandParameter="{Binding Path=NamePlaylist}" />
    </i:EventTrigger>
  </i:Interaction.Triggers>
<TreeViewItem.ItemTemplate>
   <DataTemplate DataType="{x:Type local:PlaylistDB}">
      <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding Path=NamePlaylist}">
        </TextBlock>
      </StackPanel>
    </DataTemplate>
  </TreeViewItem.ItemTemplate>
</TreeViewItem>
功能

public void DoubleClickTest(object obj)
        {
            var tmp = obj as string;
            Console.WriteLine(tmp);
        }
所以我需要修改我的类CommandProvider以获取参数,对吗?我怎么能做到

CommandProvider

public class CommandProvider : ICommand
{
#region Constructors       

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

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

#endregion

#region ICommand Members

public event EventHandler CanExecuteChanged;

public bool CanExecute(object parameter)
{
    return _canExecute != null ? _canExecute(parameter) : true;
}

public void Execute(object parameter)
{
    if (_execute != null)
        _execute(parameter);
}

public void OnCanExecuteChanged()
{
    CanExecuteChanged(this, EventArgs.Empty);
}

#endregion

private readonly Action<object> _execute = null;
private readonly Predicate<object> _canExecute = null;
}

我想在我的函数
DoubleClickTest()
中检索
NamePlaylist
,并将其传递到
CommandParameter
。如何才能做到这一点?

使用下面的类接受
命令参数
使用
ICommand

public class DelegateCommand: ICommand
{
    #region Constructors       

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

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

    #endregion

    #region ICommand Members

    public event EventHandler CanExecuteChanged;

    public bool CanExecute(object parameter)
    {
        return _canExecute != null ? _canExecute(parameter) : true;
    }

    public void Execute(object parameter)
    {
        if (_execute != null)
            _execute(parameter);
    }

    public void OnCanExecuteChanged()
    {
        CanExecuteChanged(this, EventArgs.Empty);
    }

    #endregion

    private readonly Action<object> _execute = null;
    private readonly Predicate<object> _canExecute = null;
}

obj
是在上面的示例中传递的
命令参数。

好的,我会尝试,但我如何调用它。像
public ICommand NewPlaylist{get{return new CommandProvider(obj=>DoubleClickTest(),MyBindingParameter?;}}
MyBindingParameter可以是字符串?bindingParameters可以是任何对象。您将收到作为对象的参数。检查我的编辑。我会编辑我的问题,因为它有点难。是的,我用你的类替换我的类
CommandProvider
。但是在您的使用中,调用函数在哪里?因为obj是
参数
,closesesessioncommand是
绑定
CloseSelectedSession
是将参数作为obj的方法。(obj)=>
CloseSelectedSession(obj)
public class PlaylistDB
    {
        public string NamePlaylist { get; set; }
    }
public class DelegateCommand: ICommand
{
    #region Constructors       

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

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

    #endregion

    #region ICommand Members

    public event EventHandler CanExecuteChanged;

    public bool CanExecute(object parameter)
    {
        return _canExecute != null ? _canExecute(parameter) : true;
    }

    public void Execute(object parameter)
    {
        if (_execute != null)
            _execute(parameter);
    }

    public void OnCanExecuteChanged()
    {
        CanExecuteChanged(this, EventArgs.Empty);
    }

    #endregion

    private readonly Action<object> _execute = null;
    private readonly Predicate<object> _canExecute = null;
}
public ICommand CloseCommand
    {
        get
        {
            return new DelegateCommand((obj)=>CloseMethod(obj));
        }
    }