自定义附加命令

问题描述:

我有一种情况,我想为框架元素编写自定义命令。 这是我喜欢做如下:自定义附加命令

public class UndoRedoManager 
    { 
     private static FrameworkElement frameworkElement; 

     /// <summary> 
     /// UndoVMCommand Attached properrty. 
     /// </summary> 
     public static readonly DependencyProperty UndoVMCommandProperty = 
      DependencyProperty.RegisterAttached("UndoVMCommand", typeof(ICommand), typeof(UndoRedoManager), new FrameworkPropertyMetadata(UndoVmCommand, UndoVMCommand_PropertyChanged)); 

     /// <summary> 
     /// UndoVMCommandProperty getter. 
     /// </summary> 
     /// <param name="obj"></param> 
     /// <returns></returns> 
     [AttachedPropertyBrowsableForChildren] 
     public static ICommand GetUndoVMCommand(DependencyObject obj) 
     { 
      return (ICommand)obj.GetValue(UndoVMCommandProperty); 
     } 

     /// <summary> 
     /// UndoVMCommandProperty setter. 
     /// </summary> 
     /// <param name="obj"></param> 
     /// <param name="value"></param> 
     public static void SetUndoVMCommand(DependencyObject obj, ICommand value) 
     { 
      obj.SetValue(UndoVMCommandProperty, value); 
     } 

     protected static void UndoVMCommand_PropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) 
     { 
      var control = obj as FrameworkElement; 
      if (control != null) 
      { 
       if ((e.NewValue != null) && (e.OldValue == null)) 
       { 
        frameworkElement = control; 
       } 
       else if ((e.NewValue == null) && (e.OldValue != null)) 
       { 
        frameworkElement = null; 
       } 
      } 
     } 
} 

这个我在XAML从而连接:

<ItemsControl x:Name="graphControl" local:UndoRedoManager.UndoVMCommand="{Binding UndoCommand}"> 
...... 
</ItemsControl> 

不过,我想点击一个按钮启动此命令。

<Button Content="Undo" CommandTarget="{Binding ElementName=graphControl}" Command="TheCommand" Margin="5"/> 

非常喜欢

<Button Command="Copy" CommandTarget="{Binding ElementName=MyTextBox1}">Copy</Button> 

所以我写了下面的:

public static RoutedUICommand undoVmCommand = new RoutedUICommand("TheCommand", "TheCommand", typeof(UndoRedoManager)); 
     public static RoutedUICommand UndoVmCommand 
     { 
      get { return undoVmCommand; } 
     } 
     static UndoRedoManager() 
     { 
      CommandManager.RegisterClassCommandBinding(typeof(UndoRedoManager), new CommandBinding(undoVmCommand, ExecutedEventHandler_UndoVM, CanExecuteEventHandler_IfCanUndoVM)); 
     } 

     private static void CanExecuteEventHandler_IfCanUndoVM(Object sender, CanExecuteRoutedEventArgs e) 
     { 
      FrameworkElement frmEle = sender as FrameworkElement; 
      e.CanExecute = false; 
      if (null != frmEle && frmEle.DataContext is ViewModelBase) 
      { 
       e.CanExecute = true; 
        //(frmEle.DataContext as GraphViewModel).CanUndo; 
      } 
      e.Handled = true; 
     } 

     public static void ExecutedEventHandler_UndoVM(Object sender, ExecutedRoutedEventArgs e) 
     { 
      FrameworkElement frmEle = sender as FrameworkElement; 

      if (null != frmEle && frmEle.DataContext is ViewModelBase) 
      { 
       (frmEle.DataContext as GraphViewModel).UndoCommand.Execute(null); 
      } 
     } 

我没有得到它如何连线,。我应该在哪里声明上面的路由命令?当然,我不能在FrameworkElement类中做到这一点。有没有办法来附加? 对不起,如果我没有能够清楚地说明问题。简单地说: 如果我要以附加方式为文本框编写“复制”命令,我该怎么做?

编辑:@ ERTI的评论后:

现在我有两个班,UndoRedoManager

public class UndoRedoManager 
    { 
     private static FrameworkElement frameworkElement; 

     /// <summary> 
     /// UndoVMCommand Attached properrty. 
     /// </summary> 
     public static readonly DependencyProperty UndoVMCommandProperty = 
      DependencyProperty.RegisterAttached("UndoVMCommand", typeof(ICommand), typeof(UndoRedoManager), new FrameworkPropertyMetadata(StaticCommand.UndoVmCommand, UndoVMCommand_PropertyChanged)); 

     /// <summary> 
     /// UndoVMCommandProperty getter. 
     /// </summary> 
     /// <param name="obj"></param> 
     /// <returns></returns> 
     [AttachedPropertyBrowsableForChildren] 
     public static ICommand GetUndoVMCommand(DependencyObject obj) 
     { 
      return (ICommand)obj.GetValue(UndoVMCommandProperty); 
     } 

     /// <summary> 
     /// UndoVMCommandProperty setter. 
     /// </summary> 
     /// <param name="obj"></param> 
     /// <param name="value"></param> 
     public static void SetUndoVMCommand(DependencyObject obj, ICommand value) 
     { 
      obj.SetValue(UndoVMCommandProperty, value); 
     } 

     protected static void UndoVMCommand_PropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) 
     { 
      var control = obj as FrameworkElement; 
      if (control != null) 
      { 
       if ((e.NewValue != null) && (e.OldValue == null)) 
       { 
        frameworkElement = control; 
       } 
       else if ((e.NewValue == null) && (e.OldValue != null)) 
       { 
        frameworkElement = null; 
       } 
      } 
     } 
    } 

请注意在PropertyMetadata通过在注册依赖属性的默认值。 在XAML我使用它作为:

<ItemsControl x:Name="graphControl" local:UndoRedoManager.UndoVMCommand="{Binding UndoCommand}"/> 

另一个是StaticCommand类:

public class StaticCommand 
    { 
     public static RoutedUICommand undoVmCommand = new RoutedUICommand("TheCommand", "TheCommand", typeof(UndoRedoManager)); 
     public static RoutedUICommand UndoVmCommand 
     { 
      get { return undoVmCommand; } 
     } 
     static StaticCommand() 
     { 
      CommandManager.RegisterClassCommandBinding(typeof(StaticCommand), new CommandBinding(undoVmCommand, ExecutedEventHandler_UndoVM, CanExecuteEventHandler_IfCanUndoVM)); 
     } 

     private static void CanExecuteEventHandler_IfCanUndoVM(Object sender, CanExecuteRoutedEventArgs e) 
     { 
      //This is not getting hit. 
      e.CanExecute = true; 
      e.Handled = true; 
     } 

     public static void ExecutedEventHandler_UndoVM(Object sender, ExecutedRoutedEventArgs e) 
     { 
      //I will do something here 
     } 
    } 

在XAML中,我使用它作为:

<Button Content="Undo" CommandTarget="{Binding ElementName=graphControl}" Command="{Binding Source={x:Static local:StaticCommand.UndoVmCommand}}" Margin="5"/> 

但现在上面的按钮根本没有被激活。

你基本上拥有它。只需创建新的类StaticCommands.cs。

所有你需要做的就是,现在结合对static命令,因为这样的:

<Button Command="{x:Static local:StaticCommands.UndoVmCommand}" 
    CommandTarget="{Binding WHatever you want}" /> 
在ExecutedEventHandler_UndoVM

现在,您可以访问按钮元素,这意味着你还可以访问CommandTarget属性,如果需要。

看看这个:WPF Commands, How to declare Application level commands?

+0

嗨@Erti谢谢。现在按钮从未启用。令我沮丧的是,CanExecuteEventHandler_IfCanUndoVM从未受到影响。我已经编辑了完整图片的问题。 – James 2013-04-05 08:57:31

+0

尝试Command =“{x:Static local:StaticCommand.UndoVmCommand}”,而不是绑定它。我更新了我的帖子。 – 2013-04-05 18:20:47