view Chronosv2/source/Extensions/Windows/FrameworkElementExtensions.cs @ 21:dfc81f8bb838

working version for sttocks except ui within metrowpf
author adminsh@apollo
date Tue, 20 Mar 2012 15:07:31 +0000
parents 09d18d6e5f40
children
line wrap: on
line source

using System;
using System.Threading;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;

namespace Chronos.Extensions.Windows
{
    public static class FrameworkElementExtensions
    {
        /// <summary>
        /// Set focus to UIElement
        /// </summary>
        /// <param name="element">The element to set focus on</param>
        public static void SetFocus(this FrameworkElement element)
        {
            //Focus in a callback to run on another thread, ensuring the main 
            //UI thread is initialized by the time focus is set
            ThreadPool.QueueUserWorkItem
            (
                delegate(object theElement)
                {
                    FrameworkElement elem = (FrameworkElement)theElement;

                    elem.InvokeAsynchronouslyInBackground
                    (
                        () =>
                        {
                            bool result = elem.Focus();
                            Keyboard.Focus(elem);
                        }
                    );
                }, element
            );
        }

        public static void MoveFocus(this IInputElement element, FocusNavigationDirection direction)
        {
            //Focus in a callback to run on another thread, ensuring the main 
            //UI thread is initialized by the time focus is set
            ThreadPool.QueueUserWorkItem
            (
                delegate(object theElement)
                {
                    FrameworkElement elem = (FrameworkElement)theElement;

                    elem.InvokeAsynchronouslyInBackground
                    (
                        () =>
                        {
                            elem.Focus();
                            Keyboard.Focus(elem);
                            elem.MoveFocus(new TraversalRequest(direction));
                        }
                    );
                }, element
            );
        }
    }
}