(X) Hide this
    • Login
    • Join
      • Say No Bots Generate New Image
        By clicking 'Register' you accept the terms of use .
        Login with Facebook

XAML Default Button

(0 votes)
1 comments   /   posted by Silverlight Show on Sep 04, 2009
Tags:   attached-properties , button , xaml , wpf
If you want to have a default button clicked when an enter key is pressed in a textbox within a Silverlight or WPF application then read this post of David Justice and find the solution. 

I was working with a client that wanted to have a default button clicked when an enter key is pressed in a textbox (password box to be more specific) within a Silverlight or WPF application. I didn't think much of it. I'll just listen to the event, bind to the button, and click the button on the enter key event. The listening and binding portion is not very tough with an attached property and a little Silverlight 3 element to element binding love. The tricky part came in when I wanted to cause the button click event to be raised.


Comments

  • -_-

    RE: XAML Default Button


    posted by Jeff Scherrer on Apr 14, 2010 03:49

    This works better for what we're used to for default buttons:

     

    public class ButtonInvocation : DependencyObject
    {
        public static readonly DependencyProperty OKButtonProperty = DependencyProperty.RegisterAttached("OKButton", typeof(Button), typeof(ButtonInvocation), new PropertyMetadata(null, ButtonPropertyChanged));
        public static readonly DependencyProperty CancelButtonProperty = DependencyProperty.RegisterAttached("CancelButton", typeof(Button), typeof(ButtonInvocation), new PropertyMetadata(null, ButtonPropertyChanged));
        public static readonly DependencyProperty IgnoreEnterKeyPressedProperty = DependencyProperty.RegisterAttached("IgnoreEnterKeyPressed", typeof(Boolean), typeof(ButtonInvocation), new PropertyMetadata(false));
        public static readonly DependencyProperty IgnoreEscapeKeyPressedProperty = DependencyProperty.RegisterAttached("IgnoreEscapeKeyPressed", typeof(Boolean), typeof(ButtonInvocation), new PropertyMetadata(false));
        public static Button GetOKButton(UIElement Element)
        {
            return (Button)Element.GetValue(OKButtonProperty);
        }
        public static void SetOKButton(UIElement Element, Button value)
        {
            Element.SetValue(OKButtonProperty, value);
        }
        public static Button GetCancelButton(UIElement Element)
        {
            return (Button)Element.GetValue(CancelButtonProperty);
        }
        public static void SetCancelButton(UIElement Element, Button value)
        {
            Element.SetValue(CancelButtonProperty, value);
        }
        public static Boolean GetIgnoreEnterKeyPressed(UIElement Element)
        {
            return (Boolean)Element.GetValue(IgnoreEnterKeyPressedProperty);
        }
        public static void SetIgnoreEnterKeyPressed(UIElement Element, Boolean value)
        {
            Element.SetValue(IgnoreEnterKeyPressedProperty, value);
        }
        public static Boolean GetIgnoreEscapeKeyPressed(UIElement Element)
        {
            return (Boolean)Element.GetValue(IgnoreEscapeKeyPressedProperty);
        }
        public static void SetIgnoreEscapeKeyPressed(UIElement Element, Boolean value)
        {
            Element.SetValue(IgnoreEscapeKeyPressedProperty, value);
        }
        private static void ButtonPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            Button tOKButton = null;
            Button tCancelButton = null;
            Boolean tRemovedHandler = false;
            if (e.Property == OKButtonProperty)
            {
                tOKButton = (Button)e.OldValue;
                tCancelButton = GetCancelButton((UIElement)sender);
            }
            else if (e.Property == CancelButtonProperty)
            {
                tOKButton = GetOKButton((UIElement)sender);
                tCancelButton = (Button)e.OldValue;
            }
            if (tOKButton != null || tCancelButton != null)
            {
                UIElement tSender = (UIElement)sender;
                tSender.RemoveHandler(UIElement.KeyDownEvent, (KeyEventHandler)KeyDownHandler);
                tSender.GotFocus -= UIElement_GotFocus;
                tRemovedHandler = true;
            }
            if (e.Property == OKButtonProperty)
            {
                tOKButton = (Button)e.NewValue;
            }
            else if (e.Property == CancelButtonProperty)
            {
                tCancelButton = (Button)e.NewValue;
            }
            if (tOKButton != null || tCancelButton != null)
            {
                UIElement tSender = (UIElement)sender;
                tSender.AddHandler(UIElement.KeyDownEvent, (KeyEventHandler)KeyDownHandler, true);
                tSender.GotFocus += UIElement_GotFocus;
                tSender.LostFocus += new RoutedEventHandler(UIElement_LostFocus);
            }
            else if (!tRemovedHandler)
            {
                UIElement tSender = (UIElement)sender;
                tSender.RemoveHandler(UIElement.KeyDownEvent, (KeyEventHandler)KeyDownHandler);
                tSender.GotFocus -= UIElement_GotFocus;
            }
        }
        private static void UIElement_GotFocus(object sender, RoutedEventArgs e)
        {
            UIElement tUIElement = e.OriginalSource as UIElement;
            Button tOKButton = GetOKButton((UIElement)sender);
            if (((tUIElement != null && !GetIgnoreEnterKeyPressed(tUIElement)) || tUIElement == null) && tOKButton != null)
                VisualStateManager.GoToState(tOKButton, "Focused", true);
        }
        private static void UIElement_LostFocus(object sender, RoutedEventArgs e)
        {
            UIElement tUIElement = e.OriginalSource as UIElement;
            Button tOKButton = GetOKButton((UIElement)sender);
            if (((tUIElement != null && !GetIgnoreEnterKeyPressed(tUIElement)) || tUIElement == null) && tOKButton != null)
                VisualStateManager.GoToState(tOKButton, "Normal", true);
        }
        private static void KeyDownHandler(Object sender, KeyEventArgs e)
        {
            System.Windows.Automation.Peers.ButtonAutomationPeer tButtonAutomationPeer = null;
            if (e.Key == Key.Enter)
            {
                UIElement tUIElement = e.OriginalSource as UIElement;
                Button tOKButton = GetOKButton((UIElement)sender);
                if (((tUIElement != null && !GetIgnoreEnterKeyPressed(tUIElement)) || tUIElement == null) && tOKButton != null)
                    tButtonAutomationPeer = new System.Windows.Automation.Peers.ButtonAutomationPeer(tOKButton);
            }
            else if (e.Key == Key.Escape)
            {
                UIElement tUIElement = e.OriginalSource as UIElement;
                Button tCancelButton = GetCancelButton((UIElement)sender);
                if (((tUIElement != null && !GetIgnoreEscapeKeyPressed(tUIElement)) || tUIElement == null) && tCancelButton != null)
                    tButtonAutomationPeer = new System.Windows.Automation.Peers.ButtonAutomationPeer(tCancelButton);
            }
            if (tButtonAutomationPeer != null)
            {
                System.Windows.Automation.Provider.IInvokeProvider tInvokeProvider = tButtonAutomationPeer.GetPattern(System.Windows.Automation.Peers.PatternInterface.Invoke) as System.Windows.Automation.Provider.IInvokeProvider;
                e.Handled = true;
                if (tInvokeProvider != null)
                    tInvokeProvider.Invoke();
            }
        }
    }

Add Comment

Login to comment:
  *      *       
Login with Facebook

Our News on Twitter