如何在按钮上使用命令属性来选择视图模型中的动作?

问题描述

我正在尝试在按钮上使用 Commandparameter 以便我可以使用一个命令处理程序处理该命令。我想在 if 语句中使用 commandparameter 来确定操作。

我的 Xaml 代码

<Button Content="Trending" 
        HorizontalAlignment="Left" 
        VerticalAlignment="Top" 
        Width="75" 
        Command="{Binding ClickCommand}" 
        CommandParameter="Trending" />

<Button Content="Search" 
        HorizontalAlignment="Left" 
        VerticalAlignment="Top" 
        Width="75" 
        Margin="75,0" 
        Command="{Binding ClickCommand}" 
        CommandParameter="Search"/>

我的 Commandhanlder 实现了 iCommand

public class CommandHandler : ICommand
{
    public event EventHandler CanExecuteChanged;
    private Action _action;
    private bool _canExecute;

    public CommandHandler(Action action,bool canExecute)
    {
        _action = action;
        _canExecute = canExecute;
    }

    public bool CanExecute(object parameter)
    {   
        return _canExecute;
    }

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

视图模型:

private ICommand _clickCommand;
public ICommand ClickCommand
{
    get
    {
        return _clickCommand ?? (_clickCommand = new CommandHandler(() => MyAction(),true));
    }
}

public void MyAction()
{   
    //here a if statement with Commandparameter
}

我不知道如何获得它。这甚至是正确的方法吗? 任何帮助表示赞赏!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)