2010-03-09 3 views
0

다른 ListBox가 포함 된 ItemTemplate이있는 ObservableCollection에 바인딩 된 ListBox가 있습니다. 의 항목의 DataTemplate을, 예를 들어,SelectionChanged 자식 ListBox

public object SelectedItem 
{ 
    get { return this.selectedItem; } 
    set 
    { 
     this.selectedItem = value; 
     base.NotifyPropertyChanged("SelectedItem"); 
    } 
} 

그래서 : 우선, 내 MainWindowViewModel에서 이런 식으로 (부모와 내부 사람 중 하나)를 모든리스트 박스의 마지막 선택 항목을 얻기 위해 노력 상위 목록 상자 나있어이 :

<ListBox ItemsSource="{Binding Tails}" 
SelectedItem="{Binding Path=DataContext.SelectedItem, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/> 

문제 지금되어 나는 부모 목록 상자에서 항목 다음 자식 목록 상자에서 항목을 선택하면, 내가 얻을이 :

http://i40.tinypic.com/j7bvig.jpg

보시다시피 두 개의 항목이 동시에 선택됩니다. 어떻게 해결할 수 있습니까?

미리 감사드립니다.

답변

0

ListBox 컨트롤의 SelectedEvent에 대한 ClassHandler를 등록하여이 문제를 이미 해결했습니다.

난 그냥 내 MainWindow를 클래스의 생성자이 추가 :

EventManager.RegisterClassHandler(typeof(ListBox), 
      ListBox.SelectedEvent, 
      new RoutedEventHandler(this.ListBox_OnSelected)); 

그런 식으로, 내 ListBox_OnSelected 이벤트 핸들러가 목록 상자가 호출 될 때마다 호출됩니다 및 컨트롤의 이벤트 핸들러 전에 자체라고 . MainWindowViewModel에서

나는 하나가 선택되는 추적합니다 SelectedListBox라는 속성이 있습니다

public System.Windows.Controls.ListBox SelectedListBox 
{ 
    get { return this.selectedListBox; } 
    set 
    { 
     if (this.selectedListBox != null) 
     { 
      this.selectedListBox.UnselectAll(); 
     } 
     this.selectedListBox = value; 
    } 
} 

간단한 인 selectionchanged 이벤트 핸들러를 사용하지 않는 이유는 무엇입니까? 위의 코드에서 목록 상자를 선택 취소 할 때마다 동일한 이벤트가 다시 발생하므로 다행히 WPF가 멈출 수있는 무한 루프가 발생합니다.

관련 문제