2009-04-30 7 views
8

아무도 프로그래밍 방식으로 WPF 응용 프로그램의 모든 UI 요소 탭 중지를 탐색하는 방법을 말해 줄 수 있습니까? 나는 첫 번째 탭 스톱으로 해당 엘리먼트를 스니핑하고, 다음 탭 스톱을 방문하고, 해당 엘리먼트를 스 니프하고, 마지막 탭 스톱에 도달 할 때까지 계속하고 싶다.WPF UI 요소 탭을 프로그래밍 방식으로 탐색하는 방법?

감사합니다, - 마이크

답변

27

당신이 그 초점에 대한 모든 설명이 MSDN 문서에서와 같이 MoveFocus를 사용하여 : Focus Overview합니다.

다음 집중된 요소로 이동하기위한 샘플 코드는 다음과 같습니다 (약간 수정 한 기사에서 가져옴).

// MoveFocus takes a TraversalRequest as its argument. 
TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next); 

// Gets the element with keyboard focus. 
UIElement elementWithFocus = Keyboard.FocusedElement as UIElement; 

// Change keyboard focus. 
if (elementWithFocus != null) 
{ 
    elementWithFocus.MoveFocus(request); 
} 
+0

genial, 정말 고마워요! – lamarmora

1

MoveFocus 호출로이 작업을 수행 할 수 있습니다. FocusManager를 통해 현재 포커스 된 항목을 가져올 수 있습니다. 다음 코드는 윈도우의 모든 객체를 반복하여 목록에 추가합니다. 포커스를 전환하여 윈도우를 물리적으로 수정합니다. 윈도우가 활성화되어 있지 않으면 코드가 작동하지 않을 가능성이 높습니다.

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

위의 코드는 내 WPF 창에서 작동하지 않았습니다. 목록이 비어있게됩니다. 위의 첫 번째 GetFocusedElement() 호출은 null을 반환합니다. 나는이 코드가 문서와 정확하게 일치한다는 것에 동의하지만, 불행히도 그것은 나를 위해 작동하지 않았다. 나는 이유를 알아 내기 위해 파고있다. –

+1

어디에서 코드를 호출합니까? 창은 활성 상태 여야 생성자가 확실히 생성됩니다. OnLoad가 작동 할 수도 있는데, Activated는 창을 활성화 할 때마다 호출됩니다. –