Click here to Skip to main content
12,455,701 members (95,044 online)
Click here to Skip to main content

Stats

94.5K views
3K downloads
39 bookmarked
Posted

Using the WPF FocusScope

, 26 Jul 2009 MIT
Explains why WPF seems to break if you try to use FocusScope, and provides a simple solution.
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;

namespace TestApp
{
	/// <summary>
	/// Attached behavior for improved focus scope handling.
	/// </summary>
	public static class EnhancedFocusScope
	{
		public static void SetFocusOnActiveElementInScope(UIElement scope)
		{
			IInputElement focusedElement = FocusManager.GetFocusedElement(scope);
			if (focusedElement != null) {
				Keyboard.Focus(focusedElement);
			} else {
				scope.MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
			}
		}
		
		public static bool GetIsEnhancedFocusScope(UIElement element)
		{
			return (bool)element.GetValue(IsEnhancedFocusScopeProperty);
		}
		
		public static void SetIsEnhancedFocusScope(UIElement element, bool value)
		{
			element.SetValue(IsEnhancedFocusScopeProperty, value);
		}
		
		public static readonly DependencyProperty IsEnhancedFocusScopeProperty =
			DependencyProperty.RegisterAttached(
				"IsEnhancedFocusScope",
				typeof(bool),
				typeof(EnhancedFocusScope),
				new UIPropertyMetadata(false, OnIsEnhancedFocusScopeChanged));
		
		static void OnIsEnhancedFocusScopeChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
		{
			UIElement item = depObj as UIElement;
			if (item == null)
				return;
			
			if ((bool)e.NewValue) {
				FocusManager.SetIsFocusScope(item, true);
				item.GotKeyboardFocus += OnGotKeyboardFocus;
			} else {
				FocusManager.SetIsFocusScope(item, false);
				item.GotKeyboardFocus -= OnGotKeyboardFocus;
			}
		}
		
		static void OnGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
		{
			IInputElement focusedElement = e.NewFocus;
			for (DependencyObject d = focusedElement as DependencyObject; d != null; d = VisualTreeHelper.GetParent(d)) {
				if (FocusManager.GetIsFocusScope(d)) {
					d.SetValue(FocusManager.FocusedElementProperty, focusedElement);
					if (!(bool)d.GetValue(IsEnhancedFocusScopeProperty)) {
						break;
					}
				}
			}
		}
	}
}

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 MIT License

Share

About the Author

Daniel Grunwald
Germany Germany
I am the lead developer on the SharpDevelop open source project.

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160826.1 | Last Updated 27 Jul 2009
Article Copyright 2009 by Daniel Grunwald
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid