문제

나는 월요일의 바보 같은 고통을 겪을 수도 있지만, 코드 뒤에 코드를 추가 한 후 모든 트리 뷰 노드 (treeview.expandall ()과 같은 것)을 추가 한 후 모든 트리 뷰 노드를 확장하는 멋진 방법을 찾을 수는 없습니다.

빠른 도움이 있습니까?

도움이 되었습니까?

해결책

XAML에서는 다음과 같이 할 수 있습니다.

 <TreeView.ItemContainerStyle>
            <Style TargetType="TreeViewItem">
                <Setter Property="TreeViewItem.IsExpanded" Value="True"/>
            </Style>
 </TreeView.ItemContainerStyle>

다른 팁

트리 뷰를 완전히 확장하고 붕괴시키기위한 다양한 방법을 모두 가지고 놀면 가장 빠른 방법은 다음과 같습니다. 이 방법은 매우 큰 나무에서 작동하는 것 같습니다.

나무가 가상화되지 않으면 나무가 어떤 종류의 크기에 도달하자마자, 당신이하는 일이 무엇이든 고통스럽게 느려질 것입니다.

VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode="Recycling"

트리를 뒷받침하는보기 모델이 있다고 가정합니다. 해당보기 모델의 각 노드는 HierarchicalDataTemplate 필요합니다 IsExpanded 속성 (속성 변경을 구현할 필요가 없습니다). 이보기 모델이 다음과 같은 인터페이스를 구현한다고 가정합니다.

interface IExpandableItem : IEnumerable
{
    bool IsExpanded { get; set; }
}

그만큼 TreeViewItem 스타일을 바인딩하려면 다음과 같이 설정해야합니다. IsExpanded 보기 모델의 속성 :보기 :

<Style
    TargetType="{x:Type TreeViewItem}">
    <Setter
        Property="IsExpanded"
        Value="{Binding
            IsExpanded,
            Mode=TwoWay}" />
</Style>

우리는이 속성을 사용하여 확장 상태를 설정할 것입니다. 또한 나무가 가상화되기 때문에이 속성은 개인으로서 올바른 뷰 상태를 유지하기 위해 필요합니다. TreeViewItems는 재활용됩니다. 이 바인딩 노드가 없으면 사용자가 트리를 탐색하면서 볼 수 없을 때 무너질 것입니다.

큰 나무에서 허용되는 속도를 얻는 유일한 방법은 뷰 레이어에서 코드로 작업하는 것입니다. 계획은 기본적으로 다음과 같습니다.

  1. 전류 결합을 보유하십시오 TreeView.ItemsSource.
  2. 그 바인딩을 명확하게하십시오.
  3. 바인딩이 실제로 깨끗해지기를 기다립니다.
  4. 확장 상태를 (현재 언 바운드)보기 모델로 설정하십시오.
  5. 반점 TreeView.ItemsSource 결합을 사용하여 1 단계에서 캐시했습니다.

가상화가 활성화되어 있기 때문에 바인딩을 수행합니다. TreeView.ItemsSource 큰 뷰 모델에서도 매우 빠른 것으로 밝혀졌습니다. 마찬가지로, 결합되지 않은 업데이트가 업데이트 될 때 노드의 확장 상태가 매우 빠르야합니다. 이로 인해 놀랍게도 빠른 업데이트가 발생합니다.

다음은 몇 가지 코드입니다.

void SetExpandedStateInView(bool isExpanded)
{
    var model = this.DataContext as TreeViewModel;
    if (model == null)
    {
        // View model is not bound so do nothing.
        return;
    }

    // Grab hold of the current ItemsSource binding.
    var bindingExpression = this.TreeView.GetBindingExpression(
        ItemsControl.ItemsSourceProperty);
    if (bindingExpression == null)
    {
        return;
    }

    // Clear that binding.
    var itemsSourceBinding = bindingExpression.ParentBinding;
    BindingOperations.ClearBinding(
    this.TreeView, ItemsControl.ItemsSourceProperty);

    // Wait for the binding to clear and then set the expanded state of the view model.
    this.Dispatcher.BeginInvoke(
        DispatcherPriority.DataBind, 
        new Action(() => SetExpandedStateInModel(model.Items, isExpanded)));

    // Now rebind the ItemsSource.
    this.Dispatcher.BeginInvoke(
        DispatcherPriority.DataBind,
        new Action(
            () => this.TreeView.SetBinding(
                ItemsControl.ItemsSourceProperty, itemsSourceBinding)));
}

void SetExpandedStateInModel(IEnumerable modelItems, bool isExpanded)
{
    if (modelItems == null)
    {
        return;
    }

    foreach (var modelItem in modelItems)
    {
        var expandable = modelItem as IExpandableItem;
        if (expandable == null)
        {
            continue;
        }

        expandable.IsExpanded = isExpanded;
        SetExpandedStateInModel(expandable, isExpanded);
    }
}

WPF에는 Expandall 방법이 없습니다. 각 노드에 고리를 통과하고 속성을 설정해야합니다.

보다 이 질문 또는 이 블로그 게시물.

나는 당신의 트리가 가상화 (재활용 아이템)로 설정된 경우에도 작동하는 Expandall을 수행했습니다.

이것은 내 코드입니다. 아마도 계층 구조를 계층 적 모델보기로 포장하는 것을 고려해야합니까?

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Threading;
using HQ.Util.General;

namespace HQ.Util.Wpf.WpfUtil
{
    public static class TreeViewExtensions
    {
        // ******************************************************************
        public delegate void OnTreeViewVisible(TreeViewItem tvi);
        public delegate void OnItemExpanded(TreeViewItem tvi, object item);
        public delegate void OnAllItemExpanded();

        // ******************************************************************
        private static void SetItemHierarchyVisible(ItemContainerGenerator icg, IList listOfRootToNodeItemPath, OnTreeViewVisible onTreeViewVisible = null)
        {
            Debug.Assert(icg != null);

            if (icg != null)
            {
                if (listOfRootToNodeItemPath.Count == 0) // nothing to do
                    return;

                TreeViewItem tvi = icg.ContainerFromItem(listOfRootToNodeItemPath[0]) as TreeViewItem;
                if (tvi != null) // Due to threading, always better to verify
                {
                    listOfRootToNodeItemPath.RemoveAt(0);

                    if (listOfRootToNodeItemPath.Count == 0)
                    {
                        if (onTreeViewVisible != null)
                            onTreeViewVisible(tvi);
                    }
                    else
                    {
                        if (!tvi.IsExpanded)
                            tvi.IsExpanded = true;

                        SetItemHierarchyVisible(tvi.ItemContainerGenerator, listOfRootToNodeItemPath, onTreeViewVisible);
                    }
                }
                else
                {
                    ActionHolder actionHolder = new ActionHolder();
                    EventHandler itemCreated = delegate(object sender, EventArgs eventArgs)
                        {
                            var icgSender = sender as ItemContainerGenerator;
                            tvi = icgSender.ContainerFromItem(listOfRootToNodeItemPath[0]) as TreeViewItem;
                            if (tvi != null) // Due to threading, it is always better to verify
                            {
                                SetItemHierarchyVisible(icg, listOfRootToNodeItemPath, onTreeViewVisible);

                                actionHolder.Execute();
                            }
                        };

                    actionHolder.Action = new Action(() => icg.StatusChanged -= itemCreated);
                    icg.StatusChanged += itemCreated;
                    return;
                }
            }
        }

        // ******************************************************************
        /// <summary>
        /// You cannot rely on this method to be synchronous. If you have any action that depend on the TreeViewItem 
        /// (last item of collectionOfRootToNodePath) to be visible, you should set it in the 'onTreeViewItemVisible' method.
        /// This method should work for Virtualized and non virtualized tree.
        /// The difference with ExpandItem is that this one open up the tree up to the target but will not expand the target itself,
        /// while ExpandItem expand the target itself.
        /// </summary>
        /// <param name="treeView">TreeView where  an item has to be set visible</param>
        /// <param name="listOfRootToNodePath">Any collectionic List. The collection should have every objet of the path to the targeted item from the root
        /// to the target. For example for an apple tree: AppleTree (index 0), Branch4, SubBranch3, Leaf2 (index 3)</param>
        /// <param name="onTreeViewVisible">Optionnal</param>
        public static void SetItemHierarchyVisible(this TreeView treeView, IEnumerable<object> listOfRootToNodePath, OnTreeViewVisible onTreeViewVisible = null)
        {
            ItemContainerGenerator icg = treeView.ItemContainerGenerator;
            if (icg == null)
                return; // Is tree loaded and initialized ???

            SetItemHierarchyVisible(icg, new List<object>(listOfRootToNodePath), onTreeViewVisible);
        }

        // ******************************************************************
        private static void ExpandItem(ItemContainerGenerator icg, IList listOfRootToNodePath, OnTreeViewVisible onTreeViewVisible = null)
        {
            Debug.Assert(icg != null);

            if (icg != null)
            {
                if (listOfRootToNodePath.Count == 0) // nothing to do
                    return;

                TreeViewItem tvi = icg.ContainerFromItem(listOfRootToNodePath[0]) as TreeViewItem;
                if (tvi != null) // Due to threading, always better to verify
                {
                    listOfRootToNodePath.RemoveAt(0);

                    if (!tvi.IsExpanded)
                        tvi.IsExpanded = true;

                    if (listOfRootToNodePath.Count == 0)
                    {
                        if (onTreeViewVisible != null)
                            onTreeViewVisible(tvi);
                    }
                    else
                    {
                        SetItemHierarchyVisible(tvi.ItemContainerGenerator, listOfRootToNodePath, onTreeViewVisible);
                    }
                }
                else
                {
                    ActionHolder actionHolder = new ActionHolder();
                    EventHandler itemCreated = delegate(object sender, EventArgs eventArgs)
                        {
                            var icgSender = sender as ItemContainerGenerator;
                            tvi = icgSender.ContainerFromItem(listOfRootToNodePath[0]) as TreeViewItem;
                            if (tvi != null) // Due to threading, it is always better to verify
                            {
                                SetItemHierarchyVisible(icg, listOfRootToNodePath, onTreeViewVisible);

                                actionHolder.Execute();
                            }
                        };

                    actionHolder.Action = new Action(() => icg.StatusChanged -= itemCreated);
                    icg.StatusChanged += itemCreated;
                    return;
                }
            }
        }

        // ******************************************************************
        /// <summary>
        /// You cannot rely on this method to be synchronous. If you have any action that depend on the TreeViewItem 
        /// (last item of collectionOfRootToNodePath) to be visible, you should set it in the 'onTreeViewItemVisible' method.
        /// This method should work for Virtualized and non virtualized tree.
        /// The difference with SetItemHierarchyVisible is that this one open the target while SetItemHierarchyVisible does not try to expand the target.
        /// (SetItemHierarchyVisible just ensure the target will be visible)
        /// </summary>
        /// <param name="treeView">TreeView where  an item has to be set visible</param>
        /// <param name="listOfRootToNodePath">The collection should have every objet of the path, from the root to the targeted item.
        /// For example for an apple tree: AppleTree (index 0), Branch4, SubBranch3, Leaf2</param>
        /// <param name="onTreeViewVisible">Optionnal</param>
        public static void ExpandItem(this TreeView treeView, IEnumerable<object> listOfRootToNodePath, OnTreeViewVisible onTreeViewVisible = null)
        {
            ItemContainerGenerator icg = treeView.ItemContainerGenerator;
            if (icg == null)
                return; // Is tree loaded and initialized ???

            ExpandItem(icg, new List<object>(listOfRootToNodePath), onTreeViewVisible);
        }

        // ******************************************************************
        private static void ExpandSubWithContainersGenerated(ItemsControl ic, Action<TreeViewItem, object> actionItemExpanded, ReferenceCounterTracker referenceCounterTracker)
        {
            ItemContainerGenerator icg = ic.ItemContainerGenerator;
            foreach (object item in ic.Items)
            {
                var tvi = icg.ContainerFromItem(item) as TreeViewItem;
                actionItemExpanded(tvi, item);
                tvi.IsExpanded = true;
                ExpandSubContainers(tvi, actionItemExpanded, referenceCounterTracker);
            }
        }

        // ******************************************************************
        /// <summary>
        /// Expand any ItemsControl (TreeView, TreeViewItem, ListBox, ComboBox, ...) and their childs if any (TreeView)
        /// </summary>
        /// <param name="ic"></param>
        /// <param name="actionItemExpanded"></param>
        /// <param name="referenceCounterTracker"></param>
        public static void ExpandSubContainers(ItemsControl ic, Action<TreeViewItem, object> actionItemExpanded, ReferenceCounterTracker referenceCounterTracker)
        {
            ItemContainerGenerator icg = ic.ItemContainerGenerator;
            {
                if (icg.Status == GeneratorStatus.ContainersGenerated)
                {
                    ExpandSubWithContainersGenerated(ic, actionItemExpanded, referenceCounterTracker);
                }
                else if (icg.Status == GeneratorStatus.NotStarted)
                {
                    ActionHolder actionHolder = new ActionHolder();
                    EventHandler itemCreated = delegate(object sender, EventArgs eventArgs)
                        {
                            var icgSender = sender as ItemContainerGenerator;
                            if (icgSender.Status == GeneratorStatus.ContainersGenerated)
                            {
                                ExpandSubWithContainersGenerated(ic, actionItemExpanded, referenceCounterTracker);

                                // Never use the following method in BeginInvoke due to ICG recycling. The same icg could be 
                                // used and will keep more than one subscribers which is far from being intended
                                //  ic.Dispatcher.BeginInvoke(actionHolder.Action, DispatcherPriority.Background);

                                // Very important to unsubscribe as soon we've done due to ICG recycling.
                                actionHolder.Execute();

                                referenceCounterTracker.ReleaseRef();
                            }
                        };

                    referenceCounterTracker.AddRef();
                    actionHolder.Action = new Action(() => icg.StatusChanged -= itemCreated);
                    icg.StatusChanged += itemCreated;

                    // Next block is only intended to protect against any race condition (I don't know if it is possible ? How Microsoft implemented it)
                    // I mean the status changed before I subscribe to StatusChanged but after I made the check about its state.
                    if (icg.Status == GeneratorStatus.ContainersGenerated)
                    {
                        ExpandSubWithContainersGenerated(ic, actionItemExpanded, referenceCounterTracker);
                    }
                }
            }
        }

        // ******************************************************************
        /// <summary>
        /// This method is asynchronous.
        /// Expand all items and subs recursively if any. Does support virtualization (item recycling).
        /// But honestly, make you a favor, make your life easier en create a model view around your hierarchy with
        /// a IsExpanded property for each node level and bind it to each TreeView node level.
        /// </summary>
        /// <param name="treeView"></param>
        /// <param name="actionItemExpanded"></param>
        /// <param name="actionAllItemExpanded"></param>
        public static void ExpandAll(this TreeView treeView, Action<TreeViewItem, object> actionItemExpanded = null, Action actionAllItemExpanded = null)
        {
            var referenceCounterTracker = new ReferenceCounterTracker(actionAllItemExpanded);
            referenceCounterTracker.AddRef();
            treeView.Dispatcher.BeginInvoke(new Action(() => ExpandSubContainers(treeView, actionItemExpanded, referenceCounterTracker)), DispatcherPriority.Background);
            referenceCounterTracker.ReleaseRef();
        }

        // ******************************************************************
    }
}

그리고

using System;
using System.Threading;

namespace HQ.Util.General
{
    public class ReferenceCounterTracker
    {
        private Action _actionOnCountReachZero = null;
        private int _count = 0;

        public ReferenceCounterTracker(Action actionOnCountReachZero)
        {
            _actionOnCountReachZero = actionOnCountReachZero;
        }

        public void AddRef()
        {
            Interlocked.Increment(ref _count);
        }

        public void ReleaseRef()
        {
            int count = Interlocked.Decrement(ref _count);
            if (count == 0)
            {
                if (_actionOnCountReachZero != null)
                {
                    _actionOnCountReachZero();
                }
            }
        }
    }
}

프로젝트에 다음 방법을 포함해야합니다.

 private void ExpandAllNodes(TreeViewItem treeItem)
    {
        treeItem.IsExpanded = true;
        foreach (var childItem in treeItem.Items.OfType<TreeViewItem>())
        {
            ExpandAllNodes(childItem);
        }
    }

그런 다음 다음과 같이 부르면됩니다.

treeView.Items.OfType<TreeViewItem>().ToList().ForEach(ExpandAllNodes);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top