【问题标题】:UWP: ListBox item not getting selected in an AppBarButton-flyoutUWP:未在 AppBarButton-flyout 中选择 ListBox 项
【发布时间】:2016-09-16 08:30:07
【问题描述】:

我遇到了一个奇怪的问题: 当我在 AppBarButton-Flyout 中包含一个 ListBox 时:

 <Page.TopAppBar>
            <CommandBar>
                <AppBarButton Icon="Add">
                <AppBarButton.Flyout>
<Flyout x:Name="TestFlyout">
                        <ListBox>
                            <ListBoxItem Content="A" />
                            <ListBoxItem Content="C" />
                            <ListBoxItem Content="D" />
                            <ListBoxItem Content="e" />
                            <ListBoxItem Content="F" />
                            <ListBoxItem Content="A" />

                        </ListBox>
                    </Flyout>
                </AppBarButton.Flyout>
            </AppBarButton>
            </CommandBar>
        </Page.TopAppBar>

项目未被选中(它们应该以蓝色突出显示)。 Button Flyout 中的相同列表框正在工作:

  <Button Content="Click me" IsEnabled="True">
            <Button.Flyout>
                <Flyout>
                    <ListBox>
                        <ListBoxItem Content="A" />
                        <ListBoxItem Content="C" />
                        <ListBoxItem Content="D" />
                        <ListBoxItem Content="e" />
                        <ListBoxItem Content="F" />
                        <ListBoxItem Content="A" />

                    </ListBox>
                </Flyout>
            </Button.Flyout>
        </Button>

起初我认为这可能是图形问题,但我尝试将SelectedItem 属性绑定到setter。但是 setter 永远不会被调用。 我只是在这里找不到我的错误。

编辑:

似乎是我的机器的问题。在其他 Windows-10 上,它就像一个魅力。

【问题讨论】:

  • 它适用于我的桌面(-> 我可以选择任何项目,并且所选项目以蓝色突出显示...)。您是否在一个普通的新 XAML 页面中尝试过?
  • @gregkalapos:我为此创建了一个空白的新应用。
  • 好的,我现在也在你的 gif 上看到了。对我来说,他们被选中了......(在这两种情况下,它们对我来说都是蓝色的)你可以上传你的视觉工作室解决方案吗?
  • @gregkalapos:谢谢,在另一台机器上试过,同样的应用程序正在运行。似乎是我的设备的问题。
  • 很高兴听到! ...这也是我最初的想法...

标签: c# listbox uwp


【解决方案1】:

AppBarButton 上将AllowFocusOnInteraction 属性设置为true

XAML 中的解决方案(适用于 Windows 10,版本 1607)

<AppBarButton x:Name="myAppBarButton"
              AllowFocusOnInteraction="True">
...
</AppBarButton>

或者,如果您的目标是 Windows 10 周年更新 (1607) build 14393 或更高版本,但应用的最低 Windows 10 版本较低,则应检查 @ 987654326@属性在平台上可用。

因此您不能在 XAML 中设置 AllowFocusOnInteraction 属性。相反,在代码隐藏中执行:

C# 代码隐藏解决方案

if (Windows.Foundation.Metadata.ApiInformation.IsPropertyPresent("Windows.UI.Xaml.FrameworkElement", "AllowFocusOnInteraction"))
     myAppBarButton.AllowFocusOnInteraction = true;

您还可以将其包装到附加属性中,即使在所有 Windows 10 版本上也可以在 XAML 中使用。

更多信息

您在 Windows 10 周年更新 (1607) 版本 14393 上遇到了新功能

这对于大多数应用栏使用来说是一种改进,但会干扰您的使用,因此当您将构建更改为 14393 而不是 10586 时,您需要覆盖默认值。

这是一篇博文ComboBox on a Flyout attached to an AppBarButton loses mouse input on 1607。它还包含附加的属性实现。

【讨论】:

    猜你喜欢
    • 2020-01-11
    • 2015-12-10
    • 1970-01-01
    • 2017-02-27
    • 1970-01-01
    • 2019-06-01
    • 2017-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多