コード例 #1
0
        protected override void OnKeyUp(KeyEventArgs e)
        {
            base.OnKeyUp(e);
            if (!e.Handled)
            {
                Key key = e.Key;
                switch (key)
                {
                case Key.Left:
                case Key.Right:
                case Key.Up:
                case Key.Down:
                case Key.Add:
                case Key.Subtract:
                case Key.Space:
                    IEnumerable <TreeViewItem> items = TreeViewElementFinder.FindAll(ParentTreeView, false);
                    TreeViewItem focusedItem         = items.FirstOrDefault(x => x.IsFocused);

                    focusedItem?.BringIntoView(new Rect(1, 1, 1, 1));

                    break;
                }
            }
        }
コード例 #2
0
ファイル: TreeView.cs プロジェクト: ddevelopment/paradox
        // TODO: This method has been implemented with a lot of fail and retry, and should be cleaned.
        // TODO: Also, it is probably close to work with virtualization, but it needs some testing
        public bool BringItemToView(object item, Func <object, object> getParent)
        {
            // Useful link: https://msdn.microsoft.com/en-us/library/ff407130%28v=vs.110%29.aspx
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }
            if (getParent == null)
            {
                throw new ArgumentNullException(nameof(getParent));
            }
            if (IsVirtualizing)
            {
                throw new InvalidOperationException("BringItemToView cannot be used when the tree view is virtualizing.");
            }

            TreeViewItem container = null;

            var path = new List <object> {
                item
            };
            var parent = getParent(item);

            while (parent != null)
            {
                path.Add(parent);
                parent = getParent(parent);
            }

            for (int i = path.Count - 1; i >= 0; --i)
            {
                if (container != null)
                {
                    container = (TreeViewItem)container.ItemContainerGenerator.ContainerFromItem(path[i]);
                }
                else
                {
                    container = (TreeViewItem)ItemContainerGenerator.ContainerFromItem(path[i]);
                }

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

                container.IsExpanded = true;
                container.ApplyTemplate();
                var itemsPresenter = (ItemsPresenter)container.Template.FindName("ItemsHost", container);
                if (itemsPresenter == null)
                {
                    // The Tree template has not named the ItemsPresenter,
                    // so walk the descendents and find the child.
                    itemsPresenter = container.FindVisualChildOfType <ItemsPresenter>();
                    if (itemsPresenter == null)
                    {
                        container.UpdateLayout();
                        itemsPresenter = container.FindVisualChildOfType <ItemsPresenter>();
                    }
                }
                if (itemsPresenter == null)
                {
                    return(false);
                }

                itemsPresenter.ApplyTemplate();
                var itemsHostPanel = (Panel)VisualTreeHelper.GetChild(itemsPresenter, 0);
                itemsHostPanel.UpdateLayout();
                itemsHostPanel.ApplyTemplate();

                // Ensure that the generator for this panel has been created.
                // ReSharper disable once UnusedVariable
                UIElementCollection children = itemsHostPanel.Children;
                container.BringIntoView();
            }
            return(true);
        }