依存性注入を使用してコマンドを解決するカスタムマークアップ拡張機能があります。ビューモデルでコマンドを作成したり、それらのバインディングを作成したりする必要がないため、非常に便利です。最近、このようなマークアップ拡張機能を使用することはmvvmでの良い習慣ではないと言われましたが、それは避けるべきです。本当?
マークアップ拡張のコード:
public class InjectCommandExtension : MarkupExtension
{
    #region Props
    [ConstructorArgument("key")]
    public string Key { get; set; }
    #endregion
    #region ctor
    public InjectCommandExtension()
    {
    }
    public InjectCommandExtension(string key)
    {
        Key = key;
    }
    #endregion
    #region ProvideValue
    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        if (Key == null)
            throw new ArgumentNullException("Key");
        return ServiceLocator.Current.GetInstance<ICommand>(Key);
    }
    #endregion
}
XAMLでの使用:
<Button Content="Delete" Command="{mext:InjectCommand DeleteOrderCommand}"/>