Search code examples
c#windows-runtimefocuswinrt-xamlappbar

How do I focus a TextBox in an AppBar in a C# Metro application?


I have the following code:

<Page.BottomAppBar>
    <AppBar x:Name="MyAppBar" Height="32" IsOpen="True" Opened="MyAppBar_Opened">
        <TextBox x:Name="SearchBar" Grid.Row="2" KeyDown="SearchBar_KeyDown"/>
    </AppBar>
</Page.BottomAppBar>

The problem is that it doesn't have focus when the application opens, it also doesn't have focus when I right click twice to close and open it again.

I currently have the following code in both MyAppBar_Opened and OnNavigatedTo:

if (MyAppBar == null)
    return;

MyAppBar.Focus(Windows.UI.Xaml.FocusState.Keyboard);

if (SearchBar == null)
    return;

SearchBar.Focus(Windows.UI.Xaml.FocusState.Keyboard);

But that doesn't seem to have any effect, other enum values like Mouse or Programmatic don't work either. What am I doing wrong that results in no activation?

I know an alternative that used to work before, but Metro FocusManager has no SetFocusedElement?


Solution

  • This works for me:

    XAML:

    <Page
        x:Class="Application5.BlankPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:Application5"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <Grid Background="{StaticResource ApplicationPageBackgroundBrush}">
            <AppBar
                x:Name="MyAppBar"
                Height="32"
                IsOpen="True"
                Opened="MyAppBar_Opened"
                Loaded="MyAppBar_Loaded"
                VerticalAlignment="Bottom">
                <TextBox
                    x:Name="SearchBar"
                    HorizontalAlignment="Stretch" />
            </AppBar>
        </Grid>
    </Page>
    

    Code behind:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using Windows.Foundation;
    using Windows.Foundation.Collections;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;
    
    // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
    
    namespace Application5
    {
        /// <summary>
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// </summary>
        public sealed partial class BlankPage : Page
        {
            public BlankPage()
            {
                this.InitializeComponent();
            }
    
            /// <summary>
            /// Invoked when this page is about to be displayed in a Frame.
            /// </summary>
            /// <param name="e">Event data that describes how this page was reached.  The Parameter
            /// property is typically used to configure the page.</param>
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
            }
    
            private void MyAppBar_Opened(object sender, object e)
            {
                if (SearchBar != null)
                    SearchBar.Focus(Windows.UI.Xaml.FocusState.Programmatic);
            }
    
            private void MyAppBar_Loaded(object sender, RoutedEventArgs e)
            {
                if (SearchBar != null)
                    SearchBar.Focus(Windows.UI.Xaml.FocusState.Programmatic);
            }
        }
    }