如何将RelayCommand(MVVM)绑定到RoutedCommand? (的CommandBinding)

时间:2014-09-12 13:08:44

标签: wpf relaycommand routed-commands

我想创建一个CommandBinding的自定义类,在执行RoutedCommand时执行我的ViewModel的RelayCommand。

目前只能创建在代码隐​​藏类中具有Executed方法的CommandBindings。 例如:

   <CommandBinding Command="ApplicationCommands.Close" Executed="CloseCommandHandler"
                CanExecute="CanExecuteHandler"/>

这需要后面代码中的CloseCommandHandler方法。

我想写下面的内容。

   <CommandBinding RoutedCommand="ApplicationCommands.Close" Command={Binding Path=CloseCommand}/>

唯一的问题是我无法找到RoutedCommands的气泡向下和向上事件。 没有

OnPreviewCommand(object command, object commandParammeter)
OnCommand(object command, object commandParammeter)

RoutedCommand泡沫向下和向上处理在哪里?

1 个答案:

答案 0 :(得分:0)

我提出了自己的解决方案。它不是最美丽的,而是它的工作。

我从ContentControl派生。新的Control有一个RoutedCommandBindings属性,它包含一个&#34;排序&#34;的列表。 RoutedCommands和RelayCommands之间的CommandBinding。

可以像这样使用。

<CSControls:RoutedCommandBinder>
   <CSControls:RoutedCommandBinder.RoutedCommandBindings>
      <CSControls:RoutedCommandBindingCollection>
         <CSControls:RoutedCommandBinding RoutedCommand="{x:Static ApplicationCommands.New}" Command="{Binding Path=AddInstanceCommand}"/>
      </CSControls:RoutedCommandBindingCollection>
   </CSControls:RoutedCommandBinder.RoutedCommandBindings>
   <CSControls:RoutedCommandBinder.Content>
      <!-- Every RoutedCommand of type ApplicationCommands.New will execute the binded RelayCommand "AddInstanceCommand-->
   </CSControls:RoutedCommandBinder.Content>
</CSControls:RoutedCommandBinder>

这是CustomControl代码。

public class RoutedCommandBinder : ContentControl
{
    public RoutedCommandBinder()
    {
        this.DataContextChanged += RoutedCommandBinder_DataContextChanged;
    }

    public static readonly DependencyProperty RoutedCommandBindingsProperty = DependencyProperty.Register("RoutedCommandBindings", typeof(RoutedCommandBindingCollection), typeof(RoutedCommandBinder), new PropertyMetadata(new RoutedCommandBindingCollection(), OnRoutedCommandBindingsChanged));
    public RoutedCommandBindingCollection RoutedCommandBindings
    {
        get { return (RoutedCommandBindingCollection)this.GetValue(RoutedCommandBindingsProperty); }
        set { SetValue(RoutedCommandBindingsProperty, value); }
    }

    private static void OnRoutedCommandBindingsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        RoutedCommandBinder binder = (RoutedCommandBinder)d;
        binder.CommandBindings.Clear();
        SetDataContextForCommandBindings(binder);
        if (e.NewValue != null)
        {
            RoutedCommandBindingCollection bindings = (RoutedCommandBindingCollection)e.NewValue;
            foreach (RoutedCommandBinding binding in bindings)
            {
                binder.CommandBindings.Add(new CommandBinding(binding.RoutedCommand, binder.RoutedCommandExecuted, binder.CanExecuteRoutedEventHandler));
            }
        }
    }

    private void RoutedCommandBinder_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        SetDataContextForCommandBindings(this);
    }

    private static void SetDataContextForCommandBindings(RoutedCommandBinder binder)
    {
        if (binder.DataContext != null && binder.RoutedCommandBindings != null)
        {
            foreach (RoutedCommandBinding binding in binder.RoutedCommandBindings)
            {
                binding.DataContext = binder.DataContext;
            }
        }
    }

    private void RoutedCommandExecuted(object sender, ExecutedRoutedEventArgs e)
    {
        RoutedCommandBinding binding = this.RoutedCommandBindings.FirstOrDefault(t => t.RoutedCommand == e.Command);
        if (binding != null)
        {
            binding.Command.Execute(e.Parameter);
        }
    }

    private void CanExecuteRoutedEventHandler(object sender, CanExecuteRoutedEventArgs e)
    {
        RoutedCommandBinding binding = this.RoutedCommandBindings.FirstOrDefault(t => t.RoutedCommand == e.Command);
        if (binding != null)
        {
            e.CanExecute = binding.Command.CanExecute(e.Parameter);
        }
    }
}

public class RoutedCommandBindingCollection : List<RoutedCommandBinding>
{ 
}

public class RoutedCommandBinding : FrameworkElement
{
    public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(RoutedCommandBinding), new PropertyMetadata(null));
    public ICommand Command
    {
        get { return (ICommand)this.GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }

    public static readonly DependencyProperty RoutedCommandProperty = DependencyProperty.Register("RoutedCommand", typeof(RoutedCommand), typeof(RoutedCommandBinding), new PropertyMetadata(null));
    public RoutedCommand RoutedCommand
    {
        get { return (RoutedCommand)this.GetValue(RoutedCommandProperty); }
        set { SetValue(RoutedCommandProperty, value); }
    }
}