Click here to Skip to main content
15,881,882 members
Articles / Desktop Programming / XAML

A Sample Silverlight 4 Application Using MEF, MVVM, and WCF RIA Services - Part 1

Rate me:
Please Sign up or sign in to vote.
4.84/5 (108 votes)
7 Jul 2011CPOL9 min read 2.1M   30.9K   298  
Part 1 of a series describing the creation of a Silverlight business application using MEF, MVVM Light, and WCF RIA Services.
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using System.Windows.Controls;
using System.Windows.Input;

namespace IssueVision.Common
{
    /// <summary>
    /// Helper class to provide the functionality of a default button
    /// </summary>
    public static class DefaultButtonHelper
    {
        #region "DefaultButton Attached Property"

        public static DependencyProperty DefaultButtonProperty =
            DependencyProperty.RegisterAttached("DefaultButton",
                                                typeof(Button),
                                                typeof(DefaultButtonHelper),
                                                new PropertyMetadata(null, DefaultButtonChanged));

        /// <summary>
        /// Get method for the attached property DefaultButton
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static Button GetDefaultButton(UIElement obj)
        {
            return (Button)obj.GetValue(DefaultButtonProperty);
        }

        /// <summary>
        /// Set method for the attached property DefaultButton
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="button"></param>
        public static void SetDefaultButton(UIElement obj, Button button)
        {
            obj.SetValue(DefaultButtonProperty, button);
        }

        #endregion "DefaultButton Attached Property"

        #region "Private Data & Method"

        private static bool fireClickEvent;

        private static void DefaultButtonChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var uiElement = d as UIElement;
            var button = e.NewValue as Button;
            if (uiElement != null && button != null)
            {
                // when KeyDown event fires with Key.Enter, set focus to the default button
                // and set fireClickEvent to true
                uiElement.KeyDown += (sender, arg) =>
                {
                    if (arg.Key == Key.Enter && button.IsEnabled)
                    {
                        fireClickEvent = true;
                        button.Focus();
                    }
                };

                // when the GetFocus event fires for  the default button, invoke the click event
                // when fireClickEvent is true
                button.GotFocus += (s, argument) =>
                {
                    // if fireClickEvent is true, this event is trigger by
                    // the KeyDown event with Key.Enter from uiElement
                    if (fireClickEvent)
                    {
                        var peer = new ButtonAutomationPeer(button);
                        if (peer.IsEnabled())
                        {
                            var invokeProv = peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider;
                            if (invokeProv != null) invokeProv.Invoke();
                        }
                        // set fireClickEvent back to false
                        fireClickEvent = false;
                    }
                };
            }
        }

        #endregion "Private Data & Method"
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Weidong has been an information system professional since 1990. He has a Master's degree in Computer Science, and is currently a MCSD .NET

Comments and Discussions