刷新其数据源后获取TreeList的状态?

时间:2019-04-15 06:24:01

标签: c# winforms devexpress xtratreelist

我有一个DevExpress Treelist,它的DataSource是对象列表。我将新项目添加到列表中,然后使用这段代码treelist1.RefreshDataSource();刷新Treelist DataSource。 刷新后,所有展开和选定的节点都将崩溃,这很烦人。 刷新TreeList的数据源后如何获取其状态?

2 个答案:

答案 0 :(得分:2)

我建议您看一下以下示例: https://docs.microsoft.com/en-us/dotnet/framework/xaml-services/x-shared-attribute 主要思想是在刷新数据之前保存树列表的状态,并在刷新数据后加载状态。

答案 1 :(得分:2)

流此代码:

public class TreeListViewState
    {
        private ArrayList expanded;
        private ArrayList selected;
        private object focused;
        private int topIndex;

        public TreeListViewState()
            : this(null)
        {
        }
        public TreeListViewState(TreeList treeList)
        {
            this.treeList = treeList;
            expanded = new ArrayList();
            selected = new ArrayList();
        }

        public void Clear()
        {
            expanded.Clear();
            selected.Clear();
            focused = null;
            topIndex = 0;
        }
        private ArrayList GetExpanded()
        {
            var op = new OperationSaveExpanded();
            TreeList.NodesIterator.DoOperation(op);
            return op.Nodes;
        }
        private ArrayList GetSelected()
        {
            var al = new ArrayList();
            foreach (TreeListNode node in TreeList.Selection)
            {
                al.Add(node.GetValue(TreeList.KeyFieldName));
            }
            return al;
        }

        public void LoadState()
        {
            TreeList.BeginUpdate();
            try
            {
               // TreeList.CollapseAll();
                TreeListNode node;
                foreach (object key in expanded)
                {
                    node = TreeList.FindNodeByKeyID(key);
                    if (node != null)
                    {
                        node.Expanded = true;
                    }
                }
                foreach (object key in selected)
                {
                    node = TreeList.FindNodeByKeyID(key);
                    if (node != null)
                    {
                        TreeList.Selection.Add(node);
                    }
                }
                TreeList.FocusedNode = TreeList.FindNodeByKeyID(focused);
            }
            finally
            {
                TreeList.EndUpdate();
                TreeList.TopVisibleNodeIndex = TreeList.GetVisibleIndexByNode(TreeList.FocusedNode) - topIndex;
            }
        }
        public void SaveState()
        {
            if (TreeList.FocusedNode != null)
            {
                expanded = GetExpanded();
                selected = GetSelected();
                focused = TreeList.FocusedNode[TreeList.KeyFieldName];
                topIndex = TreeList.GetVisibleIndexByNode(TreeList.FocusedNode) - TreeList.TopVisibleNodeIndex;
            }
            else
            {
                Clear();
            }
        }

        private TreeList treeList;
        public TreeList TreeList
        {
            get
            {
                return treeList;
            }
            set
            {
                treeList = value;
                Clear();
            }
        }

        private class OperationSaveExpanded : TreeListOperation
        {
            private ArrayList al = new ArrayList();
            public override void Execute(TreeListNode node)
            {
                if (node.HasChildren && node.Expanded)
                {
                    al.Add(node.GetValue(node.TreeList.KeyFieldName));
                }
            }
            public ArrayList Nodes
            {
                get
                {
                    return al;
                }
            }
        }
    }