TreeView会折叠除selected和parent之外的所有节点

时间:2013-01-29 19:59:11

标签: c# wpf treeview datatemplate

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

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

现在你的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>

答案 1 :(得分:0)

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

的Xaml:

<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;
}