自定义附加命令

时间:2013-04-05 07:08:24

标签: wpf mvvm attached-properties icommand routed-commands

我有一种情况,我想为框架元素编写自定义命令。 我在下面做了哪些:

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"/>

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

1 个答案:

答案 0 :(得分:0)

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

所有你需要做的就是,现在绑定静态命令,如下:

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

现在,在ExecutedEventHandler_UndoVM中,您可以访问Button元素,这意味着如果需要,您还可以访问CommandTarget属性。

也请查看:WPF Commands, How to declare Application level commands?