CommandParameter如何在MVVM中运行?

时间:2015-12-14 17:21:51

标签: c# wpf mvvm

我想在我的类commandprovider中实现commandparameter,它用于命令(Button等等)并从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>

控制台说,找不到NamePlaylist。

将一个函数链接到Binding NewPlaylist

public ICommand NewPlaylist { get { return new CommandProvider((obj) => DoubleClickTest(obj)); } }

功能

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

PlaylistDB

public class PlaylistDB
    {
        public string NamePlaylist { get; set; }
    }

所以我想在我的函数NamePlaylist中检索DoubleClickTest(),所以我想在CommandParameter中传递它。我怎么能这样做?

1 个答案:

答案 0 :(得分:3)

使用以下类接受commandparameters使用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;
}

用法:

public ICommand CloseCommand
    {
        get
        {
            return new DelegateCommand((obj)=>CloseMethod(obj));
        }
    }

obj是上例中传递的command parameter