2009-04-30 36 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)); 
      }; 
     } 
    } 
} 

我知道這是真的遲了迴應,但在這是否幫助你所有?

相關問題