Skip to content

Instantly share code, notes, and snippets.

@prof79
Created April 18, 2016 22:27
Show Gist options
  • Save prof79/47fe2363645b1912ee3eb03c4a971546 to your computer and use it in GitHub Desktop.
Save prof79/47fe2363645b1912ee3eb03c4a971546 to your computer and use it in GitHub Desktop.
WPF UI Focus Behavior Class for MVVM Use (e. g. Caliburn.Micro)
//----------------------------------------------------------------------------
// <copyright file="FocusBehavior.cs"
// company="Johnny CH">
// Copyright (C) 2012 Johnny CH. All rights reserved.
// </copyright>
// <author>Johnny CH</author>
// <description>A behavior class for UI focus control</description>
// <version>v1.0.0 3/13/2016 5:23:35 PM</version>
//
// Based on: https://social.msdn.microsoft.com/Forums/vstudio/en-US/8b119426-bb3b-4b5b-91e9-7827dfc92771/mvvm-wpf-focus-uilelement-from-viewmodel?forum=wpf
//
//----------------------------------------------------------------------------
namespace RecursiveDescentPoC.Behaviors
{
using System;
using System.Windows;
using System.Windows.Threading;
/// <summary>
/// Provides focus management.
/// </summary>
public static class FocusBehavior
{
/// <summary>
/// The DependencyProperty for IsFocused property.
/// </summary>
public static readonly DependencyProperty IsFocusedProperty =
DependencyProperty.RegisterAttached(
"IsFocused", //EXEMPT:GLOBALIZATION
typeof(bool),
typeof(FocusBehavior),
new FrameworkPropertyMetadata(
false,
new PropertyChangedCallback(OnIsFocusedPropertyChanged)));
/// <summary>
/// Gets the value of IsFocused on the specified object
/// </summary>
/// <param name="element">
/// The <see cref="DependencyObject"/> to get the IsFocused property.
/// </param>
/// <returns>
/// The text message string for <paramref name="element"/>.
/// </returns>
public static bool GetIsFocused(DependencyObject element)
{
// TODO ArgumentCheck.IsNotNull(element, "element");
return (bool)element.GetValue(IsFocusedProperty);
}
/// <summary>
/// Sets the value of IsFocused on the specified object
/// </summary>
/// <param name="element">
/// The <see cref="DependencyObject"/> to set the IsFocused property.
/// </param>
/// <param name="value">
/// The IsFocused value for <paramref name="element"/>.
/// </param>
public static void SetIsFocused(DependencyObject element, bool value)
{
// TODO ArgumentCheck.IsNotNull(element, "element");
element.SetValue(IsFocusedProperty, value);
}
private static void OnIsFocusedPropertyChanged(DependencyObject element, DependencyPropertyChangedEventArgs args)
{
UIElement target = element as UIElement;
if (target == null)
{
return;
}
bool isFocused = (bool) args.NewValue;
if (!isFocused)
{
return;
}
// Delay the call to allow the current batch of processing to finish before we shift focus.
target.Dispatcher.BeginInvoke(
(Action) (() =>
{
if (target.Focusable)
{
target.Focus();
}
}),
DispatcherPriority.Input);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment