3

I am getting a weird issue with the template10 (https://github.com/Windows-XAML/Template10) when I am trying to put a TextBox in the AppBarButton element the space key is not working (when you press the space key nothing happens you need to wait 3/4 seconds before it starts working). However every other key is working...

Anyone have an idea of what I am doing wrong?

Here is the simple XAML code:

<AppBarButton Icon="Find" Visibility="Visible">
    <AppBarButton.Content>
        <TextBox Width="100" />
     </AppBarButton.Content>
</AppBarButton>

I get the same issue with:

 <AppBarButton Visibility="Visible">
                <AppBarButton.Content>
                    <TextBox Width="100" />
                </AppBarButton.Content>
            </AppBarButton>

@Chris W: Is this what you want me to try? it seems a but odd to add a popup element no?

<AppBarButton Visibility="Visible" Width="100">
                <AppBarButton.Content>
                    <Popup IsOpen="True" >
                        <TextBox Width="100" />
                    </Popup>
                </AppBarButton.Content>
            </AppBarButton>
Damien
  • 2,911
  • 1
  • 25
  • 47
  • 1
    So from [the docs](https://msdn.microsoft.com/library/windows/apps/windows.ui.xaml.controls.appbarbutton.aspx) - **The Content property is ignored if the Icon is set.** So I'm not sure what the caveat is here, what about plopping it in an attached PopUp instead of as Content? – Chris W. Jan 22 '16 at 19:08
  • I have updated my post, however I get the same issue even if I don't set the Icon property – Damien Jan 22 '16 at 19:39
  • BTW: your idea of putting it in a popup does work thanks =) – Damien Jan 22 '16 at 19:49
  • 1
    Yea I'm not entirely sure how to answer this one, haven't ran into the issue before. I'd have to load up a uwp proj to fiddle around to see what the result's are visually. Hence the comment lol. – Chris W. Jan 22 '16 at 20:00
  • it works its not sexy but it works =) – Damien Jan 22 '16 at 20:29

1 Answers1

2

First, this is a strange idea when the Content property is available to you. But, hey, you are the developer and you know your app. Not me.

Okay, the problem is because the AppBarButton clearly catches the space and handles it. You can overcome this with this simple approach:

<controls:PageHeader Content="Main Page">
    <AppBarButton Width="250" Padding="0">
        <AppBarButton.Template>
            <ControlTemplate>
                <TextBox Width="250" Height="32" Margin="0,8,0,0"
                            KeyUp="TextBox_KeyUp" />
            </ControlTemplate>
        </AppBarButton.Template>
    </AppBarButton>
    <AppBarButton Icon="Find" />
</controls:PageHeader>

And then this handler:

private void TextBox_KeyUp(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
{
    if (e.Key == Windows.System.VirtualKey.Space)
    {
        var textBox = sender as TextBox;
        textBox.SelectionStart = (textBox.Text += " ").Length;
    }
}

Works like a charm:

enter image description here

Best of luck!

Jerry Nixon
  • 31,313
  • 14
  • 117
  • 233