SDK AutoCompleteBox忽略IsTabStop奇怪的问题

时间:2016-01-04 20:28:47

标签: xaml silverlight keyboard silverlight-5.0 silverlight-toolkit

我有一些sdk AutoCompleteBox,我只想设置IsTabStop="False"。如果您这样做,它会被忽略,仍然会进行焦点/标签操作。

所以我深入研究了the template并发现嵌入式TextBox,并将其显式硬编码到IsTabStop=True,所以我想我可以把它拉出来,将TemplateBind拉到一个未使用的属性,如{{1}在实例级别设置它并在模板中提供默认True的setter,对吗?

虽然没有快乐,但仍然无视它。所以我尝试在模板中明确设置Tag。哪个肯定会从Tab顺序中删除它。但是,它还会禁用控件,因此它根本不会接受焦点或编辑。

我过去遇到过类似的情况,但我的技巧都没有奏效。有没有人遇到过这个?我错过了什么只是让该死的东西掉落在Tab键顺序之外?我也很困惑为什么在嵌入式TextBox上设置IsTabStop会禁用所有hittestvisibility的控件....

不确定原因在于设置为仅忽略属性,还未在任何文档中找到解释。

所以,在我走到兔子洞口太远看之前,也许另一双眼睛可能会有所帮助,因为看似那么简单,有什么想法吗?谢谢!

1 个答案:

答案 0 :(得分:1)

MainTabBarControllerDelegate内的TextBox只有在满足所有criteria for focus ownership时才能拥有键盘焦点:

  • 必须来自AutoCompleteBox
  • 必须启用
  • 必须可见
  • 必须加载
  • (如何测试:Control事件已触发/可视父级不再为空[我使用扩展方法Loaded])
  • 和...... IsLoaded必须是真的

因此,您必须让内部IsTabStop有其明确的方式。 但我们还有其他选择。 我考虑过使用TextBox并设置ContentControl。据我所知,它应该具有Control并且其所有内容的行为类似于选项卡导航链中的一个实体部分,因此理论上,当标记时,您将进入ContentControl,但永远不会进入其内容。

但我尝试了它并没有按预期工作。不知道我的想法在哪里错了。

我玩弄它并发现以下解决方案正在运行:

TabNavigation="Once"围绕它们(我们从ContentControl派生并使控制沟成为焦点FocusDitcher,但如果用户点击内部TextBox触发则不会。我们也可以为此写一个OnGotFocus。对于焦点抛弃,控件必须破坏焦点所有权的先决条件之一,例如进入禁用模式并返回启用状态(不使用IsTabStop,尝试它,失败)。

Behavior

和模板

// helper control
public class FocusCatcher : Control { }

[TemplatePart( Name = "ReversedTabSequenceEntryPointElement", Type = typeof( UIElement ) )]
public class FocusDitcher : ContentControl
{
    public FocusDitcher()
    {
        DefaultStyleKey = typeof( FocusDitcher );
        TabNavigation = KeyboardNavigationMode.Once;
    }

    private UIElement m_reversedTabSequenceEntryPoint;

    protected override void OnGotFocus( RoutedEventArgs e )
    {
        if (FocusManager.GetFocusedElement() == this)
        {
            DitchFocus();
        }
    }

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        m_reversedTabSequenceEntryPoint = (UIElement) GetTemplateChild( "ReversedTabSequenceEntryPointElement" );
        m_reversedTabSequenceEntryPoint.GotFocus += OnReversedTabSequenceEntryPointGotFocus;
    }

    private void OnReversedTabSequenceEntryPointGotFocus( object sender, RoutedEventArgs e )
    {
        // tweak tab index to ensure that when we ditch the focus it will go to the first one in the tab order
        // otherwise it would not be possible to ever shift+tab back to any element precceeding this one in the tab order
        var localTabIndexValue = ReadLocalValue(TabIndexProperty);
        TabIndex = Int32.MinValue;
        DitchFocus();

        if (DependencyProperty.UnsetValue == localTabIndexValue)
            ClearValue( TabIndexProperty );
        else
            TabIndex = (int) localTabIndexValue;

    }

    private void DitchFocus()
    {
        IsEnabled = false; // now we lose the focus and it should go to the next one in the tab order
        IsEnabled = true;  // set the trap for next time
    }
}
相关问题