Ejemplo n.º 1
0
        /// <summary>
        /// This method is called when the selection changed event occurs. The sender should be the control
        /// on which this behaviour is attached - so we convert the sender into a <seealso cref="UIElement"/>
        /// and receive the Command through the <seealso cref="GetChangedCommand"/> getter listed above.
        ///
        /// This implementation supports binding of delegate commands and routed commands.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void Selection_Changed(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            Selector uiElement = sender as Selector;

            // Sanity check just in case this was somehow send by something else
            if (uiElement == null)
            {
                return;
            }

            ICommand changedCommand = SelectionChangedCommand.GetChangedCommand(uiElement);

            // There may not be a command bound to this after all
            if (changedCommand == null)
            {
                return;
            }

            // Check whether this attached behaviour is bound to a RoutedCommand
            if (changedCommand is RoutedCommand)
            {
                // Execute the routed command
                (changedCommand as RoutedCommand).Execute(e.AddedItems, uiElement);
            }
            else
            {
                // Execute the Command as bound delegate
                changedCommand.Execute(e.AddedItems);
            }
        }
Ejemplo n.º 2
0
        private static void uiElement_KeyUp(object sender, KeyEventArgs e)
        {
            if (e == null)
            {
                return;
            }

            // Forward key event only if user has hit the return, BackSlash, or Slash key
            if (e.Key != Key.Return)
            {
                return;
            }

            ComboBox uiElement = sender as ComboBox;

            // Sanity check just in case this was somehow send by something else
            if (uiElement == null)
            {
                return;
            }

            ICommand changedCommand = SelectionChangedCommand.GetChangedCommand(uiElement);

            // There may not be a command bound to this after all
            if (changedCommand == null)
            {
                return;
            }

            // Check whether this attached behaviour is bound to a RoutedCommand
            if (changedCommand is RoutedCommand)
            {
                // Execute the routed command
                (changedCommand as RoutedCommand).Execute(uiElement.Text, uiElement);
            }
            else
            {
                // Execute the Command as bound delegate
                changedCommand.Execute(uiElement.Text);
            }
        }