1 回答
TA贡献1818条经验 获得超11个赞
楼主可以用两种思路来解决此问题。
第一种,运用TreeView的SelectedItemChanged事件
假设TreeView控件的name是treeview
c#参考代码:
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>
- 1 回答
- 0 关注
- 2379 浏览
添加回答
举报