2009-10-12 5 views
11

WPF를 사용하는 C# ComboBox이 있습니다. ComboBoxGotFocus이 활성화 될 때 실행되는 코드가 있습니다. 문제는 GotFocus 이벤트가 ComboBox에서 선택 될 때마다 실행된다는 것입니다. 예를 들어 GotFocus은 처음에 ComboBox을 클릭 한 다음 다른 컨트롤을 클릭하지 않아도 선택하면 실행됩니다.C# ComboBox GotFocus

목록에서 선택을 수행 중이거나 GotFocus 이벤트 처리기가 결과로 실행되었는지 확인하는 데 사용할 수있는 이벤트 처리기에 플래그 또는 다른 것이 있으면이 이벤트가 발생하지 않도록 할 수 있습니까? 사용자가 목록에서 항목을 선택하고 있습니까?

+0

좋은 정보를 기능을 추가 아래의 코드에서

는 snipplet. http://stackoverflow.com/questions/830510/issue-with-wpf-focus [link] (http://stackoverflow.com/questions/830510/issue-with-wpf-focus) – Mikeb

답변

0

저는 WPF에서 너무 뜨겁지 않습니다. 그러나 새 값 등을 클릭하면 목록의 변경 사항을 감지하려고하면 SelectedIndexChanged 이벤트를 사용할 수 있습니다.

반면에 컨트롤이 집중되었을 때 정말로 알고 싶다면 다음과 같이 할 수 있습니다. 너는 뭔가를 말함으로써 그것을 걸러 낸다.

if (combo1.Focused && combo1.SelectedIndex == -1) 
{ 
    ... 
} 

..? 그것은 정말로 당신이 정확히 탐지하려고 시도하는 것에 달려 있습니다. (그들은 거품 라우팅 이벤트를 사용하기 때문에)이 코드는 항목에서 모든 포커스 이벤트를 필터링합니다

private void myComboBox_GotFocus(object sender, RoutedEventArgs e) 
{ 
    if (e.OriginalSource.GetType() == typeof(ComboBoxItem)) 
     return; 
    //Your code here 
} 

:

13

옆 검증이 문제를 해결할 수 있습니다. 그러나 WPF ComboBox 포커스의 또 다른 문제 - 특정 동작 : 항목을 사용하여 드롭 다운 목록을 열면 ComboBox에서 포커스 및 항목이 손실됩니다. 일부 항목을 선택하면 항목이 포커스를 잃고 ComboBox가 돌아옵니다. 드롭 다운 목록은 다른 컨트롤과 같습니다. 당신은 간단한 코드에 의해이를 볼 수 있습니다

private void myComboBox_GotFocus(object sender, RoutedEventArgs e) 
{ 
    if (e.OriginalSource.GetType() != typeof(ComboBoxItem)) 
    { 
     Trace.WriteLine("Got " + DateTime.Now); 
    } 
} 

private void myComboBox_LostFocus(object sender, RoutedEventArgs e) 
{ 
    if (e.OriginalSource.GetType() != typeof(ComboBoxItem)) 
    { 
     Trace.WriteLine("Lost " + DateTime.Now); 
    } 
} 

그래서 어쨌든이어야 개의 포커스 이벤트를 얻을 것이다 : 당신이 콤보 상자를 선택하고 그것에서 무언가를 선택할 때 (포커스가 콤보 상자로 돌아갑니다) 때.

항목을 선택한 후 반환 된 포커스를 필터링하려면 일부 field-flag와 함께 DropDownOpened/DropDownClosed 이벤트를 사용해보십시오.

그래서 점점 초점의 1 이벤트와 최종 코드 : 당신이 실제로 응용 프로그램에 대한 더 필요한이 예에서

private bool returnedFocus = false; 

private void myComboBox_GotFocus(object sender, RoutedEventArgs e) 
{ 
    if (e.OriginalSource.GetType() != typeof(ComboBoxItem) && !returnedFocus) 
    { 
     //Your code. 
    } 
} 

private void myComboBox_LostFocus(object sender, RoutedEventArgs e) 
{ 
    if (e.OriginalSource.GetType() != typeof(ComboBoxItem)) 
    { 
     ComboBox cb = (ComboBox)sender; 
     returnedFocus = cb.IsDropDownOpen; 
    } 
} 

선택합니다.

+0

큰 설명! –

+0

정말 고마워요. –

0

또 다른 해결 방법은 새 포커스가있는 요소가 콤보 상자의 기존 항목인지 확인하는 것입니다. true이면 콤보 상자에 아직 포커스가 있으므로 LostFocus 이벤트를 수행하면 안됩니다. 그렇지 않으면 콤보 박스 외부의 요소가 포커스를받습니다. 나는 정의 콤보 클래스에서이 다른 문제

public class MyComboBox : System.Windows.Controls.Combobox 
{ 
    protected override void OnLostFocus(RoutedEventArgs e) 
    { 
     //Get the new focused element and in case this is not an existing item of the current combobox then perform a lost focus command. 
     //Otherwise the drop down items have been opened and is still focused on the current combobox 
     var focusedElement = FocusManager.GetFocusedElement(FocusManager.GetFocusScope(this)); 
     if (!(focusedElement is ComboBoxItem && ItemsControl.ItemsControlFromItemContainer(focusedElement as ComboBoxItem) == this)) 
     { 
      base.OnLostFocus(e); 
      /* Your code here... */ 
     } 
    } 
}