Update Focused Field for FrameworkElement

image_pdfimage_print
   
 

//http://simpledbbrowser.codeplex.com/
//License:  Microsoft Public License (Ms-PL)  
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace AWS.Framework.WPF.Utility
{
    public sealed class Helpers
    {
        #region Constructor

        private Helpers()
        { }

        #endregion // Constructor

        #region Methods


        public static void UpdateFocusedField()
        {
            FrameworkElement fwe = Keyboard.FocusedElement as FrameworkElement;

            if (fwe != null)
            {
                BindingExpression expression = null;

                if (fwe is TextBox)
                    expression = fwe.GetBindingExpression(TextBox.TextProperty);
                // TODO - developers - add more control types here. Won't be that many.
                // this would include custom TextBox controls or 3rd party TextBox controls

                if (expression != null)
                    expression.UpdateSource();
            }
        }

        #endregion // Methods
    }
}