【问题标题】:Locating the first WPF tab stop定位第一个 WPF 制表位
【发布时间】:2009-04-30 23:48:06
【问题描述】:

感谢对上一个问题 (Previous Question) 的回答,我现在有一段代码可以导航 WPF 制表位(如下所示)。除了第一个制表位外,它工作正常。调用 this.MoveFocus(...First) 后跟 FocusManager.GetFocusedElement 返回 null。有任何想法吗?如何在我的窗口中获得第一个制表位?

谢谢, - 迈克

// Select the first element in the window
this.MoveFocus(new TraversalRequest(FocusNavigationDirection.First));

TraversalRequest next = new TraversalRequest(FocusNavigationDirection.Next);
List<IInputElement> elements = new List<IInputElement>();

// Get the current element.
UIElement currentElement = FocusManager.GetFocusedElement(this) as UIElement;
while (currentElement != null)
{
    elements.Add(currentElement);

    // Get the next element.
    currentElement.MoveFocus(next);
    currentElement = FocusManager.GetFocusedElement(this) as UIElement;

    // If we looped (If that is possible), exit.
    if (elements[0] == currentElement)
        break;
}

【问题讨论】:

    标签: wpf keyboard navigation tabstop


    【解决方案1】:

    我需要在我正在处理的项目中做类似的事情,我发现一些似乎工作得很好。

    这是一个带有代码的快速演示项目:

    XAML:

    <Window x:Class="WpfApplication3.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfApplication3"
            Title="MainWindow" SizeToContent="WidthAndHeight">
    
        <StackPanel>
            <TextBox Width="200" />
            <TextBox Width="200" />
            <TextBox Width="200" />
        </StackPanel>
    </Window>
    

    代码背后:

    using System.Collections.Generic;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Input;
    
    namespace WpfApplication3
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                this.InitializeComponent();
    
                // Code needs window to be active to work, so just call it in Loaded event for demo
                this.Loaded += (s, e) =>
                {
                    FocusManager.SetFocusedElement(this, this);
                    UIElement element = FocusManager.GetFocusedElement(this) as UIElement;
                    element.MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
                };
            }
        }
    }
    

    我知道这确实是迟到的反应,但这对您有帮助吗?

    【讨论】:

      猜你喜欢
      • 2012-08-25
      • 1970-01-01
      • 2010-12-08
      • 1970-01-01
      • 2016-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多