Пример #1
0
        private void ViewModelOnCatalogNavigated(object sender, bool forwardNavigation)
        {
            if (forwardNavigation)
            {
                return;
            }

            ItemsControl.Visibility = Visibility.Collapsed;
            _scrollViewer.ScrollToVerticalOffset(0);
            if (ItemsControl.ItemsSource == null || _navigationDataContexts.Count == 0)
            {
                ItemsControl.Visibility = Visibility.Visible;
                return;
            }

            var dataContext = _navigationDataContexts.Pop();

            // a little bit magic
            Dispatcher.BeginInvoke(() =>
            {
                try
                {
                    ItemsControl.BringIntoView(dataContext);
                    _scrollViewer.ScrollToVerticalOffset(_scrollViewer.VerticalOffset - _navigationVerticalOffsets.Pop());
                }
                catch (Exception)
                {
                }
                ItemsControl.Visibility = Visibility.Visible;
            });
        }
Пример #2
0
        public async Task <TreeViewItem> GetItemAsync(TModel node, IList <TModel> rootNodes)
        {
            Stack <TModel> nodes = new Stack <TModel>();

            //首先把结点从子到父放进一个站里,栈顶将是根节点
            while (!rootNodes.Contains(node))
            {
                nodes.Push(node);
                if (GetParent(node) == null)
                {
                    throw new Exception("树型数据有误");
                }
                node = GetParent(node);
            }
            ItemsControl treeViewItem = TreeView;

            while (true)
            {
                //获取该项对应的视图
                treeViewItem = treeViewItem.ItemContainerGenerator
                               .ContainerFromItem(node) as TreeViewItem;
                if (treeViewItem == null)
                {
                    return(null);
                }
                if (nodes.Count == 0)
                {
                    //说明已经获取到了目标Item
                    return(treeViewItem as TreeViewItem);
                }
                node = nodes.Pop();
                treeViewItem.BringIntoView();
                (treeViewItem as TreeViewItem).IsExpanded = true;
                await WaitForContainersGenerated(treeViewItem.ItemContainerGenerator);
            }
        }
Пример #3
0
        public bool FocusOnNode(IDurable focusRange)
        {
            if (focusRange == null)
            {
                return(false);
            }

            if (EventTreeView == null)
            {
                return(false);
            }

            if (EventTreeView.ItemsSource == null)
            {
                return(false);
            }

            List <BaseTreeNode> treePath = new List <BaseTreeNode>();

            foreach (var node in EventTreeView.ItemsSource)
            {
                BaseTreeNode baseTreeNode = node as BaseTreeNode;
                if (baseTreeNode == null)
                {
                    continue;
                }

                baseTreeNode.ForEach((curNode, level) =>
                {
                    EventNode treeEventNode = curNode as EventNode;
                    if (treeEventNode == null)
                    {
                        return(true);
                    }

                    if (treeEventNode.Entry.Start > focusRange.Finish)
                    {
                        return(false);
                    }

                    if (treeEventNode.Entry.Contains(focusRange))
                    {
                        treePath.Add(curNode);

                        //find desired node in tree
                        if (treeEventNode.Entry.Start >= focusRange.Start && treeEventNode.Entry.Finish <= focusRange.Finish)
                        {
                            return(false);
                        }
                    }


                    return(true);
                });

                ItemsControl root = EventTreeView;

                int pathElementsCount = treePath.Count;
                if (pathElementsCount > 0)
                {
                    //expand path in tree
                    int index = 0;
                    for (index = 0; index < (pathElementsCount - 1); index++)
                    {
                        BaseTreeNode expandNode = treePath[index];

                        if (root != null)
                        {
                            root = root.ItemContainerGenerator.ContainerFromItem(expandNode) as ItemsControl;
                        }

                        treePath[index].IsExpanded = true;
                    }

                    BaseTreeNode finalNode = treePath[index];

                    // select target node
                    finalNode.IsExpanded = false;
                    finalNode.IsSelected = true;

                    // focus on finalNode
                    if (root != null)
                    {
                        root = root.ItemContainerGenerator.ContainerFromItem(finalNode) as ItemsControl;
                        if (root != null)
                        {
                            root.BringIntoView();
                        }
                    }

                    EventTreeView.InvalidateVisual();

                    return(true);
                }
            }

            return(false);
        }