locked
XAML Text Box not accepting space. RRS feed

  • Question

  • I know this has been asked in some cases before, but this time it is particularly unusual. There is a textbox control that simply will not accept spacebar input. If you hold down the spacekey though, it works (of course it creates multiple spaces). It happens both with an external keyboard and the onscreen keyboard so I know it's not hardware. The thing is I have another text box on the same screen that works perfectly. I read something about PreviewKeyDown to fix this but it seems it's not supported in VS 2013 for Windows Store or something (it cannot be found). Any ideas would be appreciated.

    Thanks,

    Sam

    Sunday, October 27, 2013 4:15 AM

Answers

  • Solved! It was the fact that it's inside a button. Putting a textbox inside a button = no no. :P
    Monday, October 28, 2013 7:58 PM

All replies

  • Can you provide a repro?

    It sound like some other control on the page may be intercepting the space key.

    --Rob

    Sunday, October 27, 2013 3:28 PM
    Moderator
  • It has two events (KeyDown and LostFocus) which update a property name when they hit enter or leave textbox. For the keypress event:

    void txtTitle_KeyPressed(object sender, KeyRoutedEventArgs e)
            {
                if (e.Key == Windows.System.VirtualKey.Enter)
                {
                    UpdateRoomTitle();
                }
            }


    On another page, there is only one text box but it does the same thing.  I use a textbox on top of a text block which appears when the touch the text block so they can edit a title:

    <Button
    x:Name="pageTitle"
    AutomationProperties.Name="pageTitle"
    Click="ChangeTitle_Click"
    Style="{StaticResource TextPrimaryButtonStyle}"
    Margin="0,3,0,3"
    HorizontalAlignment="Left">
    
    <StackPanel Orientation="Horizontal">
    
    <TextBlock Name="txtblkTitle" TextWrapping="NoWrap"
    Text="{Binding Title}" FontSize="18" Width="190"          MaxHeight="30" Margin="0,0,0,0" Style="{StaticResource GroupHeaderTextStyle}" />
    
    <TextBox Name="txtTitle" KeyDown="txtTitle_KeyPressed" LostFocus="txtTitle_LostFocus" FontSize="16" Width="190" MaxHeight="30" Padding="1" Margin="0,0,0,0" Visibility="Collapsed" TextWrapping="NoWrap" Text="Test" />
    
    </StackPanel>
    
    </Button>

    Could it be that being inside of a button is messing it up?

    Thanks.

    Monday, October 28, 2013 6:49 AM
  • Solved! It was the fact that it's inside a button. Putting a textbox inside a button = no no. :P
    Monday, October 28, 2013 7:58 PM
  • Right. The button needs the space bar for activation by keyboard.

    --Rob

    Monday, October 28, 2013 8:12 PM
    Moderator