0

我想使用 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);
        //}
    }

感谢帮助。

4

2 回答 2

1

现在你的 TreeView 实际上并没有使用你的 ViewModel 的 IsExpanded 属性,因为它不知道它。尝试在项目容器样式中使用几个设置器:

<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>
于 2013-01-29T20:25:04.420 回答
0

如果你想展开/折叠所有节点,你可以试试这个

xml:

<TreeView x:Name="TreePeople">
    <TreeView.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsExpanded" Value="True" />
        </Style>
    </TreeView.ItemContainerStyle>
</TreeView> 

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;
}
于 2014-06-04T18:48:27.223 回答