我想使用 TreeView 浏览文件夹,当我选择一些文件夹和子文件夹时,我希望其他分支折叠。我试图收集一些教程,并在下面得到了几行代码。现在 TreeView 看起来像我想要的但没有崩溃。我认为问题在于属性 IsExpanded,但我不确定如何修复它并将其与 HierarchicalDataTemplate 一起使用。
这些是我绑定到 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>();
}
}
我在 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>
这是我隐藏项目的代码:
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);
//}
}
感谢帮助。