【问题标题】:Template10: AppBarButton: Content with TextBox is not allowing user to input spaceTemplate10:AppBarButton:TextBox 的内容不允许用户输入空格
【发布时间】:2023-03-04 09:15:01
【问题描述】:

当我试图在 AppBarButton 元素中放置一个 TextBox 时,我遇到了一个关于 template10 (https://github.com/Windows-XAML/Template10) 的奇怪问题,空格键不起作用(当你按下空格键时,什么都没有发生,你需要等待 3/开始工作前 4 秒)。但是其他所有键都在工作...

有人知道我做错了什么吗?

这是简单的 XAML 代码:

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

我遇到了同样的问题:

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

@克里斯 W: 这是你要我尝试的吗?添加弹出元素似乎有点奇怪?

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

【问题讨论】:

    标签: c# xaml winrt-xaml template10


    【解决方案1】:

    首先,当您可以使用Content 属性时,这是一个奇怪的想法。但是,嘿,你是开发者,你知道你的应用程序。不是我。

    好的,问题是因为AppBarButton 清楚地抓住了空间并处理了它。你可以用这个简单的方法来克服这个问题:

    <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>
    

    然后是这个处理程序:

    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;
        }
    }
    

    像魅力一样工作:

    祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-25
      • 2014-01-03
      相关资源
      最近更新 更多