示例#1
0
 /// <summary>
 /// occurs when the command change on a <see cref="DependencyObject"/>
 /// </summary>
 /// <param name="d">The dependency object.</param>
 /// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
 private static void CommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
 {
     if (d is UIElement)
     {
         var elem = (UIElement)d;
         CommandSubscription.UnregisterSubscription(e.OldValue as string, elem);
         CommandSubscription.RegisterCommand(e.NewValue as string, elem);
     }
 }
        /// <summary>
        /// Registers the command.
        /// </summary>
        /// <param name="commandName">The command name.</param>
        /// <param name="element">The element.</param>
        internal static void RegisterCommand(string commandName, UIElement element)
        {
            var subscription = new CommandSubscription(element, commandName);

            Dictionary <string, CommandSubscription> elementSubscriptions;

            if (!subscriptions.TryGetValue(element, out elementSubscriptions))
            {
                elementSubscriptions = new Dictionary <string, CommandSubscription>();
                subscriptions.Add(element, elementSubscriptions);
            }

            Command cmd = CommandService.FindCommand(commandName);

            if (cmd != null)
            {
                if (element is ButtonBase)
                {
                    ((ButtonBase)element).Click += subscription.CommandService_Click;
                }
                else if (element is TextBox)
                {
                    (element).KeyDown += subscription.CommandService_KeyDown;
                }
                else
                {
                    element.MouseLeftButtonUp += subscription.CommandService_Click;
                }

                var fe = element as FrameworkElement;
                if (fe != null)
                {
                    fe.LayoutUpdated += subscription.LayoutUpdated;
                }
            }

            elementSubscriptions[commandName] = subscription;
        }