2009-04-30 3 views
1

. Ответ на предыдущий вопрос (Previous Question). Теперь у меня есть код, который перемещает вкладки вкладок WPF (показано ниже). Он отлично работает, за исключением первой остановки табуляции. Вызов this.MoveFocus (... First), а затем FocusManager.GetFocusedElement возвращает null. Есть идеи? Как получить первую вкладку в моем окне?Поиск первой вкладки вкладки WPF

Спасибо, - Майк

// 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; 
} 

ответ

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)); 
      }; 
     } 
    } 
} 

Я знаю, что это очень поздно ответ, но это поможет вам в все?

Смежные вопросы