WPF中TreeView如何实现展开一个节点其他节点关闭?

WPF中TreeView如何实现展开一个节点其他节点关闭?


胡说叔叔
浏览 2380回答 1
1回答

慕尼黑8549860

楼主可以用两种思路来解决此问题。第一种,运用TreeView的SelectedItemChanged事件假设TreeView控件的name是treeviewc#参考代码:private void treeview_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e){TreeViewItem selected_item = treeview.SelectedItem as TreeViewItem;TreeViewItem item;if (selected_item != null){for (int i = 0; i < treeview.Items.Count; i++){item = treeview.Items[i] as TreeViewItem;if (item != selected_item){item.IsExpanded = false;}}}}第二种思路是更改TreeViewItem的style啦!~参考代码:<Style TargetType="{x:Type TreeViewItem}"><Style.Triggers><MultiTrigger><MultiTrigger.Conditions><Condition Property="IsSelected" Value="True"/><Condition Property="IsFocused" Value="True"/></MultiTrigger.Conditions><Setter Property="IsExpanded" Value="True"/></MultiTrigger><Trigger Property="IsSelected" Value="False" ><Setter Property="IsExpanded" Value="False"/></Trigger></Style.Triggers></Style>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Bootstrap