WPF Ribbon Ribbon命令替换为ICommand

WPF Ribbon Ribbon命令替换为ICommand,wpf,ribbon,Wpf,Ribbon,我正在寻找一个如何替换RibbonaApplicationMenuItem的旧代码的示例。问题是如何替换已删除的RibbonCommand <ResourceDictionary> <r:RibbonCommand x:Key="MenuItem1" CanExecute="RibbonCommand_CanExecute" LabelTitle="Menu Item 1" LabelDescription="This is

我正在寻找一个如何替换RibbonaApplicationMenuItem的旧代码的示例。问题是如何替换已删除的RibbonCommand

<ResourceDictionary>
   <r:RibbonCommand 
     x:Key="MenuItem1" 
     CanExecute="RibbonCommand_CanExecute" 
     LabelTitle="Menu Item 1" 
     LabelDescription="This is a sample menu item" 
     ToolTipTitle="Menu Item 1" 
     ToolTipDescription="This is a sample menu item" 
     SmallImageSource="Images\files.png" 
     LargeImageSource="Images\files.png" />
  </ResourceDictionary>
</r:RibbonWindow.Resources>

<r:RibbonApplicationMenuItem Command="{StaticResource MenuItem1}">
</r:RibbonApplicationMenuItem>

您可以使用
RelayCommand

这种情况下的绑定非常简单:

<ribbon:RibbonApplicationMenuItem Header="Hello _Ribbon"
                              x:Name="MenuItem1"
                              ImageSource="Images\LargeIcon.png"
                              Command="{Binding MyCommand}"
                              />
不要忘记分配
main窗口的
DataContext

public MainWindow()
{
    InitializeComponent();
    this.DataContext = new MainViewModel();
}
RelayCommand
class:

public class RelayCommand : ICommand
{
    #region Fields

    readonly Action<object> _execute;
    readonly Predicate<object> _canExecute;

    #endregion // Fields

    #region Constructors

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

    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");

        _execute = execute;
        _canExecute = canExecute;
    }
    #endregion // Constructors

    #region ICommand Members

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

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

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

    #endregion // ICommand Members
}
公共类RelayCommand:ICommand
{
#区域字段
只读操作_执行;
只读谓词_canExecute;
#endregion//字段
#区域构造函数
公共中继命令(操作执行)
:此(执行,空)
{
}
公共RelayCommand(操作执行,谓词canExecute)
{
if(execute==null)
抛出新的ArgumentNullException(“执行”);
_执行=执行;
_canExecute=canExecute;
}
#endregion//构造函数
#区域ICommand成员
公共布尔CanExecute(对象参数)
{
返回_canExecute==null?true:_canExecute(参数);
}
公共事件事件处理程序CanExecuteChanged
{
添加{CommandManager.RequerySuggested+=value;}
删除{CommandManager.RequerySuggested-=value;}
}
public void Execute(对象参数)
{
_执行(参数);
}
#endregion//ICommand成员
}

试图理解您的问题-为什么RibbonCommand已被删除?@RQDQ它已更改,现在RibbonCommand未被删除exist@kmatyazek谢谢你这个使用的好例子。干得好
public class RelayCommand : ICommand
{
    #region Fields

    readonly Action<object> _execute;
    readonly Predicate<object> _canExecute;

    #endregion // Fields

    #region Constructors

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

    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");

        _execute = execute;
        _canExecute = canExecute;
    }
    #endregion // Constructors

    #region ICommand Members

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

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

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

    #endregion // ICommand Members
}