wpf TreeView 折叠除选定节点和父节点之外的所有节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14591156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TreeView collapse all nodes except selected and parents
提问by Libor Zapletal
I want to use TreeView to browse folders and when I selected some folders and subfolders I want to other branches to collapsed. I tried to get together some tutorials and I got lines of code below. For now TreeView looks like I want but no collapsing. I think the problem is with property IsExpanded but I am not exactly sure how to fix it and use it with HierarchicalDataTemplate.
我想使用 TreeView 浏览文件夹,当我选择一些文件夹和子文件夹时,我希望其他分支折叠。我试图整理一些教程,并在下面得到了几行代码。现在 TreeView 看起来像我想要的但没有崩溃。我认为问题出在 IsExpanded 属性上,但我不确定如何修复它并将其与 HierarchicalDataTemplate 一起使用。
These are my classes binded to treeView:
这些是我绑定到 treeView 的类:
public class Item : INotifyPropertyChanged
{
public string Name { get; set; }
public string Path { get; set; }
public Item Parent { get; set; }
private bool isExpanded;
public bool IsExpanded
{
get { return isExpanded; }
set
{
isExpanded = value;
OnPropertyChanged("IsExpanded");
}
}
/// <summary>
/// Determines whether the TreeViewItem associated with this data item
/// is selected.
/// </summary>
private bool isSelected;
public bool IsSelected
{
get { return isSelected; }
set
{
isSelected = value;
OnPropertyChanged("IsSelected");
}
}
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Raises the PropertyChanged event.
/// </summary>
/// <param name="propertyName">The name of the property that has changed.</param>
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class FileItem : Item
{
}
public class DirectoryItem : Item
{
public List<Item> Items { get; set; }
public DirectoryItem()
{
Items = new List<Item>();
}
}
My treeView in XAML looks like this:
我在 XAML 中的 treeView 如下所示:
<Grid x:Name="grdFolderTree" Canvas.Left="650" Canvas.Top="540">
<TreeView x:Name="treeFolders" ItemsSource="{Binding}" SelectedItemChanged="TreeView_SelectedItemChanged" >
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type Model:DirectoryItem}" ItemsSource="{Binding Items}">
<Border Width="150" BorderBrush="RoyalBlue"
Background="RoyalBlue" BorderThickness="1"
CornerRadius="2" Margin="2" Padding="2" >
<StackPanel Orientation="Horizontal" >
<Image Width="16" Height="16"
Margin="3,0" Source="Images\TreeView\folder.png" />
<TextBlock Text="{Binding Path=Name}" FontWeight="Bold" Foreground="White"></TextBlock>
</StackPanel>
</Border>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type Model:FileItem}">
<Border Width="132" Background="LightBlue" CornerRadius="2" Margin="1" >
<StackPanel Orientation="Horizontal" >
<Image Width="16" Height="16"
Margin="3,0" Source="Images\TreeView\video.png" />
<TextBlock Margin="2" Text="{Binding Path=Name}"></TextBlock>
</StackPanel>
</Border>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
</Grid>
and this is my code for hiding items:
这是我隐藏项目的代码:
private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
List<Item> parentsOfSelectedItem = new List<Item>();
Item selectedItem = this.treeFolders.SelectedItem as Item;
if (selectedItem != null)
{
Item parentItem = selectedItem.Parent as Item;
while (parentItem != null)
{
parentsOfSelectedItem.Add(parentItem);
parentItem = parentItem.Parent as Item;
}
}
foreach (var item in this.treeFolders.Items)
{
Item treeItem = item as Item;
CollapseTreeViewItem(treeItem);
}
foreach (Item item in parentsOfSelectedItem)
{
item.IsExpanded = true;
}
if (selectedItem != null)
{
selectedItem.IsSelected = true;
selectedItem.IsExpanded = true;
}
}
void CollapseTreeViewItem(Item item)
{
item.IsExpanded = false;
//foreach (TreeViewItem subitem in item.Items)
//{
// CollapseTreeViewItem(subitem);
//}
}
Thanks for help.
感谢帮助。
回答by RogerN
Right now your TreeView isn't actually using the IsExpanded property of your ViewModel because it doesn't know about it. Try using a couple setters in the item container style:
现在您的 TreeView 实际上并没有使用您的 ViewModel 的 IsExpanded 属性,因为它不知道它。尝试在项目容器样式中使用几个 setter:
<TreeView ...>
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
回答by Darío Andrés Mu?oz Prudant
if you want to expand/collapse all nodes you can try this
如果你想展开/折叠所有节点,你可以试试这个
Xaml:
Xml:
<TreeView x:Name="TreePeople">
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="True" />
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
c#:
C#:
bool Expanded = false;
// The event subscription method (for a button click)
private void ButtonExpand__Click(object sender, RoutedEventArgs e)
{
Expanded = !Expanded;
Style Style = new Style
{
TargetType = typeof(TreeViewItem)
};
Style.Setters.Add(new Setter(TreeViewItem.IsExpandedProperty, Expanded));
TreePeople.ItemContainerStyle = Style;
}

