简体   繁体   English

UWP:在AppBarButton-flyout中未选择ListBox项

[英]UWP: ListBox item not getting selected in an AppBarButton-flyout

I have come across a strange issue: When I have a ListBox included in a AppBarButton-Flyout: 我遇到了一个奇怪的问题:当我在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>

The items get not selected (they should be highlighted in blue). 这些项目未被选中(它们应以蓝色突出显示)。 The same Listbox in a Button Flyout is working: 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>

At first I thought maybe it's a graphical issue, but I've tried to bind the SelectedItem property to a settter. 起初,我认为可能是图形问题,但是我尝试将SelectedItem属性绑定到setter。 But the setter is never called. 但是二传手从来没有被叫过。 I just can't find my mistake here. 我只是在这里找不到我的错误。

这里的问题

Edit: 编辑:

Seems to be an issue with my machine. 看来是我的机器有问题。 On other Windows-10 it's working like a charm. 在其他Windows-10上,它的运行就像是一种魅力。

Set AllowFocusOnInteraction property to true on the AppBarButton . AppBarButton AllowFocusOnInteraction属性设置为true

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

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

or if you are targeting Windows 10 Anniversary update (1607) build 14393 or higher, but the app's minimum Windows 10 version is lower , you should check if the AllowFocusOnInteraction property is available on the platform. 或者,如果您的目标是Windows 10周年更新 (1607)内部版本14393或更高版本,但该应用的最低Windows 10版本低于 ,则应检查AllowFocusOnInteraction属性在平台上是否可用。

So you can't set the AllowFocusOnInteraction property in XAML. 因此,您无法在XAML中设置AllowFocusOnInteraction属性。 Instead, do it in code-behind: 相反,请在代码背后进行操作:

Solution in C# code-behind C#代码隐藏的解决方案

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

You can also wrap it into an attached property that can be used in XAML even on all Windows 10 versions. 您还可以将其包装到一个附加属性中,即使在所有Windows 10版本中,该属性也可以在XAML中使用。

More info 更多信息

You're running into a new feature on Windows 10 Anniversary update (1607), build 14393. 您正在运行Windows 10周年更新 (1607),内部版本14393 的新功能

That's an improvement for most app bar uses but interferes with yours, so you'll need to override the default value when you change your build to rather 14393 instead of 10586. 对于大多数应用栏,这是一个改进,但会干扰您的应用栏,因此,当您将内部版本更改为14393而不是10586时,您将需要覆盖默认值。

Here's a blog post ComboBox on a Flyout attached to an AppBarButton loses mouse input on 1607 . 这是一个博客文章ComboBox,位于附加到AppBarButton的Flyout上,在1607年失去了鼠标输入 It also contains the attached property implementation. 它还包含附加的属性实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM