Flyout中ComboBox失效

参见这篇文章:https://blogs.msdn.microsoft.com/wsdevsol/2016/09/14/combobox-from-an-appbarbutton-loses-mouse-input-on-1607/

Several developers have asked why a ComboBox on a Flyout attached to an AppBarButton ignores mouse clicks after upgrading their UWP apps to target build 14393 (Windows 10, version 1607 – also known as the Windows 10 Anniversary Update). The same Xaml worked fine unchanged when the app targeted build 10586 (Windows 10, version 1511).

<tl;dr>

Apps can reenable focus here by setting the AppBarButton’s AllowFocusOnInteraction property to true.

Why is this happening?

The new behavior is a side effect of the new focus system which allows users to interact with controls such as AppBarButtons without stealing the focus from the control they’re trying to modify. This has been a huge request from devs whose apps have scenarios such as text editors: the app can have formatting buttons (e.g. bold, italic, colors) which change the formatting without affecting the text’s focus or selection.

The new behavior enhances the common AppBarButton use case so AppBarButtons default to AllowFocusOnInteraction to false if the app opts in to new behavior by targeting build 14393.

In the case where the AppBarButton brings up a ComboBox, the ComboBox depends on being able to get the focus. For this case the app needs to override the default and explicitly set AllowFocusOnInteraction to true.

How do I let the ComboBox get focus?

If you just fired up your app in Visual Studio and added AllowFocusOnInteraction to the AppBarButton Xaml you probably discovered that the editor rejected it. AllowFocusOnInteraction is only available in build 14393 and later. If the app’s minimum version is lower than that then it can’t be used in version flexible Xaml.

Set in code behind with adaptive code

The standard pattern to set properties that may or may not exist is to check if it’s present with the Windows.Foundation.Metadata.ApiInformation class:

private void AppBarButton_Loaded(object sender, RoutedEventArgs e)
{
    bool allowFocusOnInteractionAvailable =
        Windows.Foundation.Metadata.ApiInformation.IsPropertyPresent(
            "Windows.UI.Xaml.FrameworkElement",
            "AllowFocusOnInteraction");

    if (allowFocusOnInteractionAvailable)
    {
        var s = sender as FrameworkElement;
        if (s != null)
        {
            s.AllowFocusOnInteraction = true;
        }
    }
}

Set in Xaml via an Attached Property

If we don’t want to set this directly from code-behind we can bring the adaptive code back to Xaml by exposing it as an attached property. This property can be set on any AppBarButton (&c) we want to set AllowFocusOnInteraction on regardless of version. If AllowFocusOnInteraction is available then the property will set it. If not, then it’s a no-op.

public class CompatExtensions
{
    public static bool GetAllowFocusOnInteraction(DependencyObject obj)
    {
        return (bool)obj.GetValue(AllowFocusOnInteractionProperty);
    }
    public static void SetAllowFocusOnInteraction(DependencyObject obj, bool value)
    {
        obj.SetValue(AllowFocusOnInteractionProperty, value);
    }
    // Using a DependencyProperty as the backing store for AllowFocusOnInteraction.
    // This enables animation, styling, binding, etc...
    public static readonly DependencyProperty AllowFocusOnInteractionProperty =
        DependencyProperty.RegisterAttached("AllowFocusOnInteraction",
                                            typeof(bool),
                                            typeof(CompatExtensions),
                                            new PropertyMetadata(0, AllowFocusOnInteractionChanged));

    private static bool allowFocusOnInteractionAvailable =
        Windows.Foundation.Metadata.ApiInformation.IsPropertyPresent(
            "Windows.UI.Xaml.FrameworkElement",
            "AllowFocusOnInteraction");
    private static void AllowFocusOnInteractionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (allowFocusOnInteractionAvailable)
        {
            var element = d as FrameworkElement;
            if (element != null)
            {
                element.AllowFocusOnInteraction = (bool)e.NewValue;
            }
        }
    }
}

And then set the property in Xaml without worrying about the app version:

<AppBarButton local:CompatExtensions.AllowFocusOnInteraction="True" Icon="Setting">
    <AppBarButton.Flyout>
        <Flyout>
            <StackPanel Orientation="Vertical" >
                <ComboBox>
                    <ComboBoxItem Content="Red" IsSelected="True" />
                    <ComboBoxItem Content="Green" />
                    <ComboBoxItem Content="Blue"/>
                </ComboBox>
            </StackPanel>
        </Flyout>
    </AppBarButton.Flyout>
</AppBarButton>
时间: 2024-11-07 08:44:20

Flyout中ComboBox失效的相关文章

解决QML开发中ComboBox中一个已选择项没有清除的问题

解决QML开发中ComboBox中一个已选择项没有清除的问题 近期使用QML开发一个项目.须要使用ComboBox进行显示.当进行一个操作时,须要向ComboBox加入一个元素,当进行另外一个操作时.须要清除ComboBox里面的元素. 可是在操作的过程中,出现了一个诡异的现象--ComboBox里面的已选择项并没有清除. 以下是程序的截图,能够看到.ComboBox中已选择项并没有删除.可是ComboBox中的候选项已经删除了. 我在QTCN上进行提问.后面再大家的努力下,最终把这个问题攻克了

项目记录:spring+springmvc 项目中 @Transactional 失效的解决方法

第一步,修改spring的配置文件和springmvc的配置文件 --------------------------------applicationContext.xml <context:annotation-config/>  <context:component-scan base-package="com.xxx"> <context:exclude-filter type="annotation" expression=&

android 学习过程中登陆失效的个人理解

今天在学习的过程中,要做登陆失效的功能,所以就找了些资料,好好看了一下,研究了一番,慢慢的做出来了! 比如:你在一个手机端登陆了账号,在另外的一个手机端也登陆了账号,此时,前一个手机端的账号会提示登陆失效. 意思是只能存在一个账号,这个其实不是很难. 每次登陆的时候会存在一个Token,每次登陆的Token是不一样的! 下面贴一下前端的一些小代码: 在异步网络请求里面判断返回的异常是否是登陆失效: @Override protected void onPostExecute(BusinessRe

sql server数据库中索引失效的问题讨论

有关于数据库中索引失效的问题,网上也有相关的讨论.不过他们是针对oracle数据库进行讨论的.那么在sql server数据库中索引什么时候 会失效呢.总结了一下,不过我没有经过测试.没测试就没有发言权,这里仅供自己参考. 首先,所谓失效.并不真的就是这个索引被删除了.而是在这些情况下,DBMS不会检索索引列表了.执行速度和没有这个索引时的速度一样. 但是再执行另外的一条语句.同样索引可以正常起作用.所以索引的失效是针对某条sql语句的,而不是针对索引本身的.那么在哪些情况下, 确切的说是在哪类

Oracle EBS-SQL (BOM-6):检查物料失效但BOM中未失效的数据.sql

select msi.segment1                   装配件编码 , msi.description                  装配件描述 , msi.item_type                   类型 , msi.planner_code               计划员 , msi1.segment1                  部件编码 , msi1.description                部件描述 , msi1.primary

关于easyUI中combobox多选并附加checkbox的实现

在easyUI的基础组件中combobox只有带radiobutton的效果,而且只支持单选. 但是我们可以在此控件的基础上,对combobox进行重写,就能够实现checkbox多选效果. 大致需要重写以下几个方法: format : function(row){     var opts = $(this).combobox("options");     return "<input type='checkbox' class='combobox-checkbox

第三方支付过程中session失效问题

第三方支付过程中session失效问题 时间 2015-05-13 12:36:23  IT社区推荐资讯 原文  http://itindex.net/detail/53436-session-问题 主题 Java 问题产行于公司p2c交易平台的一个用户充值模块. 平台简介:该平台通过第三方支付(以下使用“支付宝”代替)完成与各大银行的交易(充值.投资.转帐.提现等操作),并将数据保存在我司的服务器数据库中,服务器使用tomcat,数据库Mysql 平台充值流程:用户登陆系统----点击充值--

记录数据库中索引失效的问题

昨天碰到一个很有意思的问题,一个sql 语句,加上 SoftUseLine like '%OQC%' 之后,速度就特别慢.去掉该条件之后,速度就快起来了. 查看sql 语句的执行情况,发现加上那个查询条件之后,SoftWareDetailInfo表的逻辑读取变成了1300374 次, 可是这个查询字段明明已经加上Index_SoftWareDetail索引了,怎么会逻辑读取还这么大呢.查看sql profile 的详细信息,发现sql 语句,没有走那个索引. 于是,加上WITH(INDEX(In

c#(winform)中ComboBox添加Key/Value项、获取选中项、根据Key

WinForm下的ComboBox默认是以多行文本来设定显示列表的, 这通常不符合大家日常的应用, 因为大家日常应用通常是键/值对的形式去绑定它的. 参考了一些网上的例子,最终写了一个辅助类用于方便对ComboBox的操作: 用下面这个类的实例作为ComboBox的添加项: using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; namespace tp7309.