Caliburn Micro: Focus TextBox when View gets visible

Caliburn Micro: Focus TextBox when View gets visible

Currently I'm using the WPF framework Caliburn Micro for my UI. With Conductors you can manage the lifecycle of views. If an item gets activated/visible I'd like to focus the TextBox to enhance the user experience. Therefor I've created a new Behaviour:

public class FocusWhenVisibleBehavior : Behavior<FrameworkElement>
{
    protected override void OnAttached()
    {
        AssociatedObject.Loaded += Loaded;
        AssociatedObject.IsVisibleChanged += VisibleChanged;
    }

    protected override void OnDetaching()
    {
        AssociatedObject.Loaded -= Loaded;
        AssociatedObject.IsVisibleChanged -= VisibleChanged;
    }

    private void Loaded(object sender, RoutedEventArgs e)
    {
        TryFocus();
    }

    private void VisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        TryFocus();
    }

    private void TryFocus()
    {
        if (AssociatedObject.IsLoaded && AssociatedObject.IsVisible)
        {
            AssociatedObject.Focus();
        }
    }
}

Now you can use it in the XAML file:

<TextBox x:Name="Input">
      <i:Interaction.Behaviors>
          <Behaviour:FocusWhenVisibleBehavior/>
      </i:Interaction.Behaviors>
</TextBox>

i is the namespace System.Windows.Interactivity which is included in the Blend SDK. So you have to install the SDK on your build server, reference a third party NuGet package or wait if Caliburn Micro will include the package (Issue #145).


Cover photo by Dimitris Kalogeropoylos / CC (BY-SA)