본문 바로가기

컴퓨터 이야기/Visual Studio

[WPF]ListBox 안에 Expander 사용할 때 하나만 펼치게 하기

WPF에서 Expander를 ListBox의 DataTemplate에 넣어서 사용하게 되면

ListBox.SelectionChanged 이벤트가 잡히지 않습니다.


그래서 구글링하면 다음과 같은 stackoverflow 글을 볼 수 있습니다.


http://stackoverflow.com/questions/1183326/wpf-listbox-expander-events


결론은 다음과 같이 xaml 코드에서 처리하면 쉽게 됩니다. 

머리를 굴리지 않아도 간단하게 되니 더욱 깔끔합니다.


IsExpanded="{Binding IsSelected,Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"