Example #1
0
        void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target)
        {
            switch (connectionId)
            {
            case 1:
                this.@this = ((TreeViewEx.VirtualizationSample.MainWindow)(target));
                return;

            case 2:

            #line 16 "..\..\..\MainWindow.xaml"
                ((System.Windows.Controls.Button)(target)).Click += new System.Windows.RoutedEventHandler(this.OnLoad);

            #line default
            #line hidden
                return;

            case 3:

            #line 17 "..\..\..\MainWindow.xaml"
                ((System.Windows.Controls.Button)(target)).Click += new System.Windows.RoutedEventHandler(this.OnClear);

            #line default
            #line hidden
                return;

            case 4:
                this.tree = ((System.Windows.Controls.TreeViewEx)(target));
                return;
            }
            this._contentLoaded = true;
        }
 /// <summary>
 /// Returns all items in tree recursively. If virtualization is enabled, only realized items are returned.
 /// </summary>
 /// <param name="treeView">The tree.</param>
 /// <param name="ignoreInvisible">True if only visible items should be returned.</param>
 /// <returns>Returns an enumerable of items.</returns>
 internal static IEnumerable<TreeViewExItem> FindAll(TreeViewEx treeView, bool ignoreInvisible)
 {
     TreeViewExItem currentItem = FindFirst(treeView, ignoreInvisible);
     while (currentItem != null)
     {
         if (GetIsVisible(currentItem)) yield return currentItem;
         currentItem = FindNext(currentItem, ignoreInvisible);
     }
 }
        public ItemFinderViewModel(TreeViewEx treeView) : base(treeView)
        {
            DragCommand = new DragCommand();
            DropCommand = new DropCommand(this);

            RemoveDirectoryCommand = new RelayCommand<object>(RemoveSelectedDirectories, CanRemoveSelectedDirectoies);
            AddNewDirectoryCommand = new RelayCommand<object>(AddNewDirectories, CanAddNewDirectory);

            _finderDirector = FinderDirector.GetInstance();
        }
        public InputEventRouter(TreeViewEx treeView)
        {
            inputSubscribers = new List<InputSubscriberBase>(2);
            this.treeView = treeView;

            treeView.MouseDown += OnMouseDown;
            treeView.MouseMove += OnMouseMove;
            treeView.MouseUp += OnMouseUp;
            treeView.ScrollViewer.ScrollChanged += OnScrollChanged;
        }
Example #5
0
        public void Dispose()
        {
            if (treeViewEx != null)
            {
                treeViewEx.Drop     -= OnDrop;
                treeViewEx.DragOver -= OnDragOver;

                treeViewEx = null;
            }
        }
Example #6
0
        public InputEventRouter(TreeViewEx treeView)
        {
            inputSubscribers = new List <InputSubscriberBase>(2);
            this.treeView    = treeView;

            treeView.MouseDown += OnMouseDown;
            treeView.MouseMove += OnMouseMove;
            treeView.MouseUp   += OnMouseUp;
            treeView.ScrollViewer.ScrollChanged += OnScrollChanged;
        }
        /// <summary>
        /// Returns the last item. If tree is virtualized, it is the last realized item.
        /// </summary>
        /// <param name="treeView">The tree.</param>
        /// <returns>Returns a TreeViewExItem.</returns>
        internal static TreeViewExItem FindLast(TreeViewEx treeView, bool ignoreInvisible)
        {
            for (int i = treeView.Items.Count - 1; i >= 0; i--)
            {
                var item = treeView.ItemContainerGenerator.ContainerFromIndex(i) as TreeViewExItem;
                if (item != null && GetIsVisible(item)) return item;
            }

            return null;
        }
Example #8
0
 void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target)
 {
     switch (connectionId)
     {
     case 1:
         this.tree = ((System.Windows.Controls.TreeViewEx)(target));
         return;
     }
     this._contentLoaded = true;
 }
        /// <summary>
        /// Returns the first item. If tree is virtualized, it is the first realized item.
        /// </summary>
        /// <param name="treeView">The tree.</param>
        /// <returns>Returns a TreeViewExItem.</returns>
        internal static TreeViewExItem FindFirst(TreeViewEx treeView, bool visibleOnly)
        {
            for (int i = 0; i < treeView.Items.Count; i++)
            {
                var item = treeView.ItemContainerGenerator.ContainerFromIndex(i) as TreeViewExItem;
                if (item == null) continue;
                if (!visibleOnly || item.IsVisible) return item;
            }

            return null;
        }
Example #10
0
        private TreeViewExItem GetFocusedItem()
        {
            foreach (var item in TreeViewEx.RecursiveTreeViewItemEnumerable(treeViewEx, false))
            {
                if (item.IsFocused)
                {
                    return(item);
                }
            }

            return(null);
        }
        public void Dispose()
        {
            if (content != null)
            {
                content.MouseDown -= OnMouseDown;
                content.MouseMove -= OnMouseMove;
                content.MouseUp   -= OnMouseUp;
                content            = null;
            }

            GC.SuppressFinalize(this);
        }
Example #12
0
        /// <summary>
        /// Returns the last item. If tree is virtualized, it is the last realized item.
        /// </summary>
        /// <param name="treeView">The tree.</param>
        /// <returns>Returns a TreeViewExItem.</returns>
        internal static TreeViewExItem FindLast(TreeViewEx treeView, bool ignoreInvisible)
        {
            for (int i = treeView.Items.Count - 1; i >= 0; i--)
            {
                var item = treeView.ItemContainerGenerator.ContainerFromIndex(i) as TreeViewExItem;
                if (item != null && GetIsVisible(item))
                {
                    return(item);
                }
            }

            return(null);
        }
Example #13
0
        /// <summary>
        /// Returns all items in tree recursively. If virtualization is enabled, only realized items are returned.
        /// </summary>
        /// <param name="treeView">The tree.</param>
        /// <param name="ignoreInvisible">True if only visible items should be returned.</param>
        /// <returns>Returns an enumerable of items.</returns>
        internal static IEnumerable <TreeViewExItem> FindAll(TreeViewEx treeView, bool ignoreInvisible)
        {
            TreeViewExItem currentItem = FindFirst(treeView, ignoreInvisible);

            while (currentItem != null)
            {
                if (GetIsVisible(currentItem))
                {
                    yield return(currentItem);
                }
                currentItem = FindNext(currentItem, ignoreInvisible);
            }
        }
 public TreeViewItemEx(TreeViewEx tv, int id, string type, string text, string tooltip, bool hasChildren)
     : base()
 {
     _treeview = tv;
     myId = id;
     myType = type;
     Header = text;
     if (tooltip != null && tooltip.Length > 0) ToolTip = tooltip;
     if (!hasChildren)
     {
         this.ContextMenuOpening += new ContextMenuEventHandler(TreeViewItemEx_ContextMenuOpening);
     }
 }
Example #15
0
        /// <summary>
        /// Returns all items in tree recursively. If virtualization is enabled, only realized items are returned.
        /// </summary>
        /// <param name="treeView">The tree.</param>
        /// <param name="visibleOnly">True if only visible items should be returned.</param>
        /// <returns>Returns an enumerable of items.</returns>
        internal static IEnumerable <TreeViewExItem> FindAll(TreeViewEx treeView, bool visibleOnly)
        {
            TreeViewExItem currentItem = FindFirst(treeView, visibleOnly);

            while (currentItem != null)
            {
                if (!visibleOnly || currentItem.IsVisible)
                {
                    yield return(currentItem);
                }
                currentItem = FindNext(currentItem, visibleOnly);
            }
        }
Example #16
0
        void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target)
        {
            switch (connectionId)
            {
            case 1:
                this.leftTree = ((System.Windows.Controls.TreeViewEx)(target));

            #line 67 "..\..\..\MainWindow.xaml"
                this.leftTree.KeyDown += new System.Windows.Input.KeyEventHandler(this.leftTree_KeyDown);

            #line default
            #line hidden
                return;
            }
            this._contentLoaded = true;
        }
        // Be sure to call the base class constructor.
        public BorderSelectionAdorner(TreeViewEx treeViewEx)
            : base(treeViewEx)
        {
            this.treeViewEx = treeViewEx;
            this.border = new Border { BorderThickness = new Thickness(1), CornerRadius = new CornerRadius(1), Opacity = 0.5 };

            Binding brushBinding = new Binding("BorderBrushSelectionRectangle");
            brushBinding.Source = treeViewEx;
            border.SetBinding(Border.BorderBrushProperty, brushBinding);
            Binding backgroundBinding = new Binding("BackgroundSelectionRectangle");
            backgroundBinding.Source = treeViewEx;
            border.SetBinding(Border.BackgroundProperty, backgroundBinding);

            layer = AdornerLayer.GetAdornerLayer(treeViewEx);
            layer.Add(this);
        }
Example #18
0
        /// <summary>
        /// Returns the first item. If tree is virtualized, it is the first realized item.
        /// </summary>
        /// <param name="treeView">The tree.</param>
        /// <returns>Returns a TreeViewExItem.</returns>
        internal static TreeViewExItem FindFirst(TreeViewEx treeView, bool visibleOnly)
        {
            for (int i = 0; i < treeView.Items.Count; i++)
            {
                var item = treeView.ItemContainerGenerator.ContainerFromIndex(i) as TreeViewExItem;
                if (item == null)
                {
                    continue;
                }
                if (!visibleOnly || item.IsVisible)
                {
                    return(item);
                }
            }

            return(null);
        }
Example #19
0
        // Be sure to call the base class constructor.
        public BorderSelectionAdorner(TreeViewEx treeViewEx)
            : base(treeViewEx)
        {
            this.border = new Border {
                BorderThickness = new Thickness(1), CornerRadius = new CornerRadius(1), Opacity = 0.5
            };
            Binding brushBinding = new Binding("BorderBrushSelectionRectangle");

            brushBinding.Source = treeViewEx;
            border.SetBinding(Border.BorderBrushProperty, brushBinding);
            Binding backgroundBinding = new Binding("BackgroundSelectionRectangle");

            backgroundBinding.Source = treeViewEx;
            border.SetBinding(Border.BackgroundProperty, backgroundBinding);

            layer = AdornerLayer.GetAdornerLayer(treeViewEx);
            layer.Add(this);
        }
Example #20
0
        public void Dispose()
        {
            if (treeView != null)
            {
                treeView.MouseDown -= OnMouseDown;
                treeView.MouseMove -= OnMouseMove;
                treeView.MouseUp   -= OnMouseUp;

                treeView = null;
            }

            if (inputSubscribers != null)
            {
                inputSubscribers.Clear();
                inputSubscribers = null;
            }

            GC.SuppressFinalize(this);
        }
Example #21
0
        TreeViewExItem GetTreeViewItemUnderMouse(TreeViewEx treeView, Point positionRelativeToTree)
        {
            HitTestResult hitTestResult = VisualTreeHelper.HitTest(treeView, positionRelativeToTree);

            if (hitTestResult == null || hitTestResult.VisualHit == null)
            {
                return(null);
            }

            TreeViewExItem   item          = null;
            DependencyObject currentObject = hitTestResult.VisualHit;

            while (item == null && currentObject != null)
            {
                item          = currentObject as TreeViewExItem;
                currentObject = VisualTreeHelper.GetParent(currentObject);
            }

            return(item);
        }
Example #22
0
        void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target)
        {
            switch (connectionId)
            {
            case 1:
                this.tb = ((System.Windows.Controls.TextBox)(target));
                return;

            case 2:
                this.tree1 = ((System.Windows.Controls.TreeViewEx)(target));
                return;

            case 3:
                this.button = ((System.Windows.Controls.Button)(target));
                return;

            case 4:
                this.tree2 = ((System.Windows.Controls.TreeView)(target));
                return;
            }
            this._contentLoaded = true;
        }
Example #23
0
        private static void OnSelectedItemsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TreeViewEx treeView = (TreeViewEx)d;

            if (e.OldValue != null)
            {
                INotifyCollectionChanged collection = e.OldValue as INotifyCollectionChanged;
                if (collection != null)
                {
                    collection.CollectionChanged -= treeView.OnSelectedItemsChanged;
                }
            }

            if (e.NewValue != null)
            {
                INotifyCollectionChanged collection = e.NewValue as INotifyCollectionChanged;
                if (collection != null)
                {
                    collection.CollectionChanged += treeView.OnSelectedItemsChanged;
                }
            }
        }
Example #24
0
        public void SelectPreviousFromKey()
        {
            List <TreeViewExItem> items = TreeViewEx.RecursiveTreeViewItemEnumerable(treeViewEx, true).ToList();
            TreeViewExItem        item;
            TreeViewExItem        focusedItem = GetFocusedItem();

            item = treeViewEx.GetPreviousItem(focusedItem, items);

            if (item == null)
            {
                return;
            }

            // if ctrl is pressed just focus it, so it can be selected by space. Otherwise select it.
            if (IsControlKeyDown)
            {
                FocusHelper.Focus(item);
            }
            else
            {
                SelectCore(item);
            }
        }
Example #25
0
 public SelectionMultiple(TreeViewEx treeViewEx)
 {
     this.treeViewEx = treeViewEx;
 }
 private void RegisterHeight(TreeViewEx tree, int level, double size)
 {
     cachedSizes.AddOrChange(0, size);
     tree.cachedSizes.AddOrChange(level, size);
 }
        /// <summary>
        /// Returns the size of the container for a given item.  The size can come from the container, a lookup, or a guess depending
        /// on the virtualization state of the item.
        /// </summary>
        /// <returns>The cached or estimated size.</returns>
        /// <remarks>This estimation looks if the given index is cached. If not it returns the maximum height of the cached
        /// containers. If no container is cached, returns zero. 
        /// One case it fails is, if all cached items are bigger
        /// than the estimated items. This leads to jumping scrollbars. The effect is not that bad, if many items will be visualized.</remarks>
        private double GetCachedOrEstimatedHeight(TreeViewEx tree, int level)
        {
            if (cachedSizes.ContainsItems(0)) return cachedSizes.GetEstimate(0);

            return tree.cachedSizes.GetEstimate(level);
        }
Example #28
0
        private void SelectCore(TreeViewExItem item)
        {
            if (IsControlKeyDown)
            {
                if (!treeViewEx.CheckSelectionAllowed(item.DataContext, true))
                {
                    return;
                }

                if (treeViewEx.SelectedItems.Contains(item.DataContext))
                {
                    throw new InvalidOperationException("The item must not be contained.");
                }

                treeViewEx.SelectedItems.Add(item.DataContext);
                item.IsSelected = true;
                lastShiftRoot   = item.DataContext;
            }
            else if (IsShiftKeyDown && treeViewEx.SelectedItems.Count > 0)
            {
                object         firstSelectedItem = lastShiftRoot ?? treeViewEx.SelectedItems.First();
                TreeViewExItem shiftRootItem     = treeViewEx.GetTreeViewItemsFor(new List <object> {
                    firstSelectedItem
                }).First();

                IEnumerable <object> items = treeViewEx.GetNodesToSelectBetween(shiftRootItem, item).Select(x => x.DataContext);

                IEnumerable <object> itemsToSelect   = GetItemsNotInCollection((IEnumerable <object>)treeViewEx.SelectedItems, items);
                IEnumerable <object> itemsToUnSelect = GetItemsNotInCollection(items, (IEnumerable <object>)treeViewEx.SelectedItems);
                if (!treeViewEx.CheckSelectionAllowed(itemsToSelect, itemsToUnSelect))
                {
                    return;
                }

                treeViewEx.SelectedItems.Clear();

                foreach (var node in TreeViewEx.RecursiveTreeViewItemEnumerable(treeViewEx, false))
                {
                    if (items.Contains(node.DataContext))
                    {
                        treeViewEx.SelectedItems.Add(node.DataContext);
                        node.IsSelected = true;
                    }
                    else
                    {
                        node.IsSelected = false;
                    }
                }
            }
            else
            {
                if (!treeViewEx.CheckSelectionAllowed(item.DataContext, true))
                {
                    return;
                }

                // check if selection is already item, otherwise set it
                if (!(treeViewEx.SelectedItems.Count == 1 && treeViewEx.SelectedItems[0] == item.DataContext))
                {
                    foreach (var treeViewItem in TreeViewEx.RecursiveTreeViewItemEnumerable(treeViewEx, false))
                    {
                        treeViewItem.IsSelected = false;
                    }

                    treeViewEx.SelectedItems.Clear();
                    treeViewEx.SelectedItems.Add(item.DataContext);
                }

                lastShiftRoot = item.DataContext;
            }

            FocusHelper.Focus(item);
        }
Example #29
0
 public IsEditingManager(TreeViewEx treeview)
 {
     this.treeview = treeview;
 }
        public void Dispose()
        {
            if (treeView != null)
            {
                treeView.MouseDown -= OnMouseDown;
                treeView.MouseMove -= OnMouseMove;
                treeView.MouseUp -= OnMouseUp;
                treeView.ScrollViewer.ScrollChanged -= OnScrollChanged;

                treeView = null;
            }

            if (inputSubscribers != null)
            {
                inputSubscribers.Clear();
                inputSubscribers = null;
            }

            GC.SuppressFinalize(this);
        }
 /// <summary>
 /// Returns all items in tree recursively. If virtualization is enabled, only realized items are returned.
 /// </summary>
 /// <param name="treeView">The tree.</param>
 /// <param name="visibleOnly">True if only visible items should be returned.</param>
 /// <returns>Returns an enumerable of items.</returns>
 internal static IEnumerable<TreeViewExItem> FindAll(TreeViewEx treeView, bool visibleOnly)
 {
     TreeViewExItem currentItem = FindFirst(treeView, visibleOnly);
     while (currentItem != null)
     {
         if (!visibleOnly || currentItem.IsVisible) yield return currentItem;
         currentItem = FindNext(currentItem, visibleOnly);
     }
 }
 public IsEditingManager(TreeViewEx treeview)
 {
     this.treeview = treeview;
 }
Example #33
0
 public FinderViewModel(TreeViewEx treeView)
 {
     SelectedNodes = new ObservableCollection<FinderNode>();
     if (treeView != null)
         treeView.OnSelecting += OnNodeSelected;
 }
        /// <summary>
        /// Measure the children
        /// </summary>
        /// <param name="availableSize">Size available</param>
        /// <returns>Size desired</returns>
        protected override Size MeasureOverride(Size availableSize)
        {
            if (ScrollOwner != null)
            {
                if (ScrollOwner.ScrollableWidth < HorizontalOffset)
                {
                    SetHorizontalOffset(ScrollOwner.ScrollableWidth);
                }
                if (ScrollOwner.ScrollableHeight < VerticalOffset)
                {
                    SetVerticalOffset(ScrollOwner.ScrollableHeight);
                }
            }

            // We need to access InternalChildren before the generator to work around a bug
            UIElementCollection     children     = InternalChildren;
            IItemContainerGenerator generator    = ItemContainerGenerator;
            ItemsControl            itemsControl = ItemsControl.GetItemsOwner(this);
            TreeViewExItem          treeViewItem = itemsControl as TreeViewExItem;
            TreeViewEx treeView = itemsControl as TreeViewEx ?? treeViewItem.ParentTreeView;

            double currentY = 0;
            double maxWidth = 0;
            int    firstVisibleItemIndex = 0;
            int    lastVisibleItemIndex  = 0;

            if (itemsControl.HasItems)
            {
                if (treeView.IsVirtualizing)
                {
                    double bottomY = VerticalOffset + availableSize.Height;

                    //add sizes of not visible items before visible ones to currentX
                    for (int i = 0; i < itemsControl.Items.Count; i++)
                    {
                        // get height, maybe it is estimated
                        double height = GetContainerHeightForItem(itemsControl, i);

                        if (currentY + height >= VerticalOffset && currentY <= bottomY)
                        {
                            firstVisibleItemIndex = i;
                            lastVisibleItemIndex  = i;

                            // we found the first visible item, lets realize it and all other visible items. while we
                            // do so, we take care of counting i up
                            GeneratorPosition startPos = generator.GeneratorPositionFromIndex(firstVisibleItemIndex);
                            int itemGeneratorIndex     = (startPos.Offset == 0) ? startPos.Index : startPos.Index + 1;

                            using (generator.StartAt(startPos, GeneratorDirection.Forward, true))
                            {
                                // create items until current y is bigger than bottomy => we reached the last visible item
                                while (currentY <= bottomY && i < itemsControl.Items.Count)
                                {
                                    // Get or create the child
                                    bool           newlyRealized;
                                    TreeViewExItem child = generator.GenerateNext(out newlyRealized) as TreeViewExItem;
                                    if (newlyRealized)
                                    {
                                        // Figure out if we need to insert the child at the end or somewhere in the middle
                                        AddOrInsertItemToInternalChildren(itemGeneratorIndex, child);
                                        child.ParentTreeView = treeView;
                                        generator.PrepareItemContainer(child);
                                    }
                                    else
                                    {
                                        // The child has already been created, let's be sure it's in the right spot
                                        if (child != children[itemGeneratorIndex])
                                        {
                                            throw new InvalidOperationException("Wrong child was generated");
                                        }
                                    }

                                    child.Measure(new Size(double.MaxValue, double.MaxValue));

                                    // now get the real height
                                    height = child.DesiredSize.Height;
                                    // add real height to cache
                                    cachedSizes.AddOrChange(i, height);
                                    // add real height to current position
                                    currentY += height;
                                    // save the maximum needed width
                                    maxWidth = Math.Max(maxWidth, child.DesiredSize.Width);

                                    lastVisibleItemIndex++;
                                    i++;
                                    itemGeneratorIndex++;

                                    //break realization if we reach the bottom of control
                                    if (currentY > bottomY)
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                        else
                        {
                            currentY += height;
                        }
                    }

                    // Note: this could be deferred to idle time for efficiency
                    CleanUpItems(firstVisibleItemIndex, lastVisibleItemIndex);
                }
                else
                {
                    GeneratorPosition startPos = generator.GeneratorPositionFromIndex(0);
                    using (generator.StartAt(startPos, GeneratorDirection.Forward, true))
                    {
                        for (int i = (startPos.Offset == 0) ? startPos.Index : startPos.Index + 1; i < itemsControl.Items.Count; i++)
                        {
                            // Get or create the child
                            bool           newlyRealized;
                            TreeViewExItem child = generator.GenerateNext(out newlyRealized) as TreeViewExItem;
                            if (newlyRealized)
                            {
                                // Figure out if we need to insert the child at the end or somewhere in the middle
                                AddOrInsertItemToInternalChildren(i, child);
                                child.ParentTreeView = treeView ?? treeViewItem.ParentTreeView;
                                generator.PrepareItemContainer(child);
                            }

                            child.Measure(new Size(double.MaxValue, double.MaxValue));
                            // now get the real height
                            double height = child.DesiredSize.Height;
                            // add real height to current position
                            currentY += height;
                            // save the maximum needed width
                            maxWidth = Math.Max(maxWidth, child.DesiredSize.Width);
                        }
                    }
                }
            }

            Extent   = new Size(maxWidth, currentY);
            Viewport = availableSize;

            return(Extent);
        }
        /// <summary>
        /// Arrange the children
        /// </summary>
        /// <param name="finalSize">Size available</param>
        /// <returns>Size used</returns>
        protected override Size ArrangeOverride(Size finalSize)
        {
            ItemsControl            itemsControl = ItemsControl.GetItemsOwner(this);
            TreeViewExItem          treeViewItem = itemsControl as TreeViewExItem;
            TreeViewEx              treeView     = itemsControl as TreeViewEx ?? treeViewItem.ParentTreeView;
            IItemContainerGenerator generator    = this.ItemContainerGenerator;

            //Extent = finalSize;
            bool   foundVisibleItem = false;;
            double currentY         = 0;

            if (treeView.IsVirtualizing)
            {
                for (int i = 0; i < itemsControl.Items.Count; i++)
                {
                    FrameworkElement child = itemsControl.ItemContainerGenerator.ContainerFromIndex(i) as FrameworkElement;

                    if (foundVisibleItem)
                    {
                        if (child == null)
                        {
                            // other items are not visible / virtualized
                            break;
                        }
                    }
                    else
                    {
                        if (child != null)
                        {
                            // found first visible item
                            foundVisibleItem = true;
                        }
                    }

                    if (child != null)
                    {
                        child.Arrange(new Rect(-HorizontalOffset, currentY - VerticalOffset, finalSize.Width, child.DesiredSize.Height));
                        currentY += child.ActualHeight;
                    }
                    else
                    {
                        currentY += GetContainerHeightForItem(itemsControl, i);
                    }
                }

                // update average after arrange, because we have to use the same average as in measure for our calculation of currentY.
                cachedSizes.Update();
            }
            else
            {
                for (int i = 0; i < itemsControl.Items.Count; i++)
                {
                    UIElement child = itemsControl.ItemContainerGenerator.ContainerFromIndex(i) as UIElement;

                    if (child != null)
                    {
                        child.Arrange(new Rect(-HorizontalOffset, currentY - VerticalOffset, finalSize.Width, child.DesiredSize.Height));
                    }
                    currentY += child.DesiredSize.Height;
                }
            }

            return(finalSize);
        }
        /// <summary>
        /// Returns the size of the container for a given item.  The size can come from the container, a lookup, or a guess depending
        /// on the virtualization state of the item.
        /// </summary>
        /// <returns>The cached or estimated size.</returns>
        /// <remarks>This estimation looks if the given index is cached. If not it returns the maximum height of the cached
        /// containers. If no container is cached, returns zero. 
        /// One case it fails is, if all cached items are bigger
        /// than the estimated items. This leads to jumping scrollbars. The effect is not that bad, if many items will be visualized.</remarks>
        private double GetCachedOrEstimatedHeight(TreeViewEx tree, int level)
        {
            if (cachedSizes.ContainsItems(0)) return cachedSizes.GetEstimate(0);

            return tree.cachedSizes.GetEstimate(level);
        }
Example #37
0
 public void ApplyTemplate()
 {
     borderSelectionLogic = new BorderSelectionLogic(
         treeViewEx,
         TreeViewEx.RecursiveTreeViewItemEnumerable(treeViewEx, false));
 }
        /// <summary>
        /// Arrange the children
        /// </summary>
        /// <param name="finalSize">Size available</param>
        /// <returns>Size used</returns>
        protected override Size ArrangeOverride(Size finalSize)
        {
            ItemsControl            itemsControl = ItemsControl.GetItemsOwner(this);
            TreeViewExItem          treeViewItem = itemsControl as TreeViewExItem;
            TreeViewEx              treeView     = itemsControl as TreeViewEx ?? treeViewItem.ParentTreeView;
            IItemContainerGenerator generator    = this.ItemContainerGenerator;

            //Extent = finalSize;
            bool   foundVisibleItem = false;;
            double currentY         = 0;

            if (treeView.IsVirtualizing)
            {
                //Debug("Arrange-" + itemsControl.DataContext);
                for (int i = 0; i < itemsControl.Items.Count; i++)
                {
                    TreeViewExItem child = itemsControl.ItemContainerGenerator.ContainerFromIndex(i) as TreeViewExItem;
                    int            childHierarchyLevel = 0;
                    if (child != null)
                    {
                        childHierarchyLevel = child.hierachyLevel;
                    }

                    if (foundVisibleItem)
                    {
                        if (child == null)
                        {
                            // other items are not visible / virtualized
                            break;
                        }
                    }
                    else
                    {
                        if (child != null)
                        {
                            // found first visible item
                            foundVisibleItem = true;
                        }
                    }

                    if (child != null)
                    {
                        child.Arrange(new Rect(-HorizontalOffset, currentY - VerticalOffset, finalSize.Width, child.DesiredSize.Height));
                        currentY += child.ActualHeight;
                    }
                    else
                    {
                        currentY += GetCachedOrEstimatedHeight(treeView, childHierarchyLevel);
                    }
                }
            }
            else
            {
                for (int i = 0; i < itemsControl.Items.Count; i++)
                {
                    UIElement child = itemsControl.ItemContainerGenerator.ContainerFromIndex(i) as UIElement;

                    if (child != null)
                    {
                        child.Arrange(new Rect(-HorizontalOffset, currentY - VerticalOffset, finalSize.Width, child.DesiredSize.Height));
                    }
                    currentY += child.DesiredSize.Height;
                }
            }

            return(finalSize);
        }
 public BorderSelectionLogic(TreeViewEx treeView, IEnumerable<TreeViewExItem> items)
 {
     this.items = items;
     TreeView = treeView;
 }
Example #40
0
 public static void Focus(TreeViewEx element)
 {
     //System.Diagnostics.Debug.WriteLine("Focus Tree with helper");
     FocusCore(element);
 }
 private void RegisterHeight(TreeViewEx tree, int level, double size)
 {
     cachedSizes.AddOrChange(0, size);
     tree.cachedSizes.AddOrChange(level, size);
 }
 public FinderViewModel CreateFinderViewModel(TreeViewEx treeView)
 {
     var fd = FinderDirector.GetInstance();
     var collection = _target == null ? fd.Collection : new ObservableCollection<FinderNode>();
     FinderViewModel = new FinderViewModel(treeView, new ObservableCollection<FinderNode>(collection));
     FinderViewModel.SelectItemsChanged += OnFinderViewSelectItemChanged;
     return FinderViewModel;
 }
 void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target) {
     switch (connectionId)
     {
     case 1:
     this.TreeView = ((System.Windows.Controls.TreeViewEx)(target));
     return;
     }
     this._contentLoaded = true;
 }
Example #44
0
 public FinderViewModel(TreeViewEx treeView, ObservableCollection<FinderNode> root) : this(treeView)
 {
     Nodes = root;
 }
 public BorderSelectionLogic(TreeViewEx treeView, IEnumerable <TreeViewExItem> items)
 {
     this.items = items;
     TreeView   = treeView;
 }
Example #46
0
        public SelectionMultiple(TreeViewEx treeViewEx)
        {
            this.treeViewEx = treeViewEx;

            BorderSelectionLogic = new BorderSelectionLogic(treeViewEx, TreeViewElementFinder.FindAll(treeViewEx, false));
        }
 public TreeViewExAutomationPeer(TreeViewEx owner)
     : base(owner)
 {
 }
        /// <summary>
        /// Measure the children
        /// </summary>
        /// <param name="availableSize">Size available</param>
        /// <returns>Size desired</returns>
        protected override Size MeasureOverride(Size availableSize)
        {
            if (ScrollOwner != null)
            {
                if (ScrollOwner.ScrollableWidth < HorizontalOffset) SetHorizontalOffset(ScrollOwner.ScrollableWidth);
                if (ScrollOwner.ScrollableHeight < VerticalOffset) SetVerticalOffset(ScrollOwner.ScrollableHeight);
            }

            // We need to access InternalChildren before the generator to work around a bug
            UIElementCollection children = InternalChildren;
            IItemContainerGenerator generator = ItemContainerGenerator;
            ItemsControl itemsControl = ItemsControl.GetItemsOwner(this);
            TreeViewExItem treeViewItem = itemsControl as TreeViewExItem;
            TreeViewEx treeView = itemsControl as TreeViewEx ?? treeViewItem.ParentTreeView;
            Debug(treeViewItem, "Measuring");
            double maxWidth = 0;
            double currentYinItemSystem = 0;

            if (treeView.IsVirtualizing)
            {
                // never forget: virtualization of a tree is an approximation. there are some use cases which theoretically work and others
                // we try to get it working by estimations. See GetCachedOrEstimatedHeight for more infos.

                int itemCount = itemsControl.Items.Count;
                int firstVisibleItemIndex = 0;
                int lastVisibleItemIndex = itemCount;

                double itemTop;
                if (treeViewItem != null)
                {
                    itemTop = treeViewItem.itemTopInTreeSystem + GetHeightOfHeader(itemsControl);
                }
                else
                {
                    // get the area where items have to be visualized. This is from top to bottom of the visible space in tree system. 
                    // We add a little of offset. It seems like it improves estimation of heights.
                    double predictionOffset = 50;
                    double top = VerticalOffset - predictionOffset;
                    if (top < 0) top = 0;
                    treeView.realizationSpace.Top = top;
                    treeView.realizationSpace.Bottom = VerticalOffset + availableSize.Height + predictionOffset;

                    itemTop = GetHeightOfHeader(itemsControl);
                }

                int itemGeneratorIndex = 0;
                bool isPreviousItemVisible = false;
                IDisposable generatorRun = null;
                currentYinItemSystem = 0;
                int childHierarchyLevel = 0;
                if(treeViewItem != null) childHierarchyLevel = treeViewItem.hierachyLevel + 1;
                try
                {
                    // iterate child items
                    for (int i = 0; i < itemCount; i++)
                    {
                        double estimatedHeight = GetCachedOrEstimatedHeight(treeView, childHierarchyLevel);
                        VerticalArea childSpace = new VerticalArea();
                        childSpace.Top = itemTop + currentYinItemSystem;
                        childSpace.Bottom = childSpace.Top + estimatedHeight;

                        // check if item is possibly visible or could become visible if someone changes expanding of siblings
                        bool isVisibleItem = treeView.realizationSpace.IsWithin(childSpace);

                        if (isVisibleItem)
                        {
                            // we have found a visible item, lets check if its the first visible item.
                            if (!isPreviousItemVisible)
                            {
                                // we found a visible item, lets initialize the visible item section of the loop
                                isPreviousItemVisible = true;
                                firstVisibleItemIndex = i;
                                GeneratorPosition startPos = generator.GeneratorPositionFromIndex(i);
                                itemGeneratorIndex = (startPos.Offset == 0) ? startPos.Index : startPos.Index + 1;
                                generatorRun = generator.StartAt(startPos, GeneratorDirection.Forward, true);
                            }
                            else
                            {
                                itemGeneratorIndex++;
                            }

                            // Get or create the child
                            bool newlyRealized;
                            TreeViewExItem child = generator.GenerateNext(out newlyRealized) as TreeViewExItem;
                            Debug(treeViewItem, "Found visible child: " + child.DataContext);

                            if (newlyRealized)
                            {
                                // Figure out if we need to insert the child at the end or somewhere in the middle
                                AddOrInsertItemToInternalChildren(itemGeneratorIndex, child);
                                child.ParentTreeView = treeView;
                                generator.PrepareItemContainer(child);
                            }
                            else
                            {
                                // The child has already been created, let's be sure it's in the right spot
                                if (child != children[itemGeneratorIndex]) throw new InvalidOperationException("Wrong child was generated");
                            }

                            if (treeViewItem != null)
                            {
                                child.itemTopInTreeSystem = currentYinItemSystem + itemTop;
                                child.hierachyLevel = treeViewItem.hierachyLevel + 1;
                            }
                            else
                            {
                                child.itemTopInTreeSystem = currentYinItemSystem;
                                child.hierachyLevel = 1;
                            }

                            InvalidateMeasure(child);
                            child.Measure(new Size(double.MaxValue, double.MaxValue));

                            // add real height to cache
                            double heightOfChild = child.DesiredSize.Height;
                            RegisterHeight(treeView, childHierarchyLevel, heightOfChild);
                            currentYinItemSystem += child.DesiredSize.Height;
                            // save the maximum needed width
                            maxWidth = Math.Max(maxWidth, child.DesiredSize.Width);
                        }
                        else
                        {
                            //Debug(treeViewItem, "Found invisible child: " + i);
                            if (isPreviousItemVisible)
                            {
                                // set last visible index. this is important, to cleanup not anymore used items
                                lastVisibleItemIndex = i;
                                isPreviousItemVisible = false;
                            }

                            // dispose generator run. if we do it after the whole loop, we run in multithreading issues
                            if (generatorRun != null)
                            {
                                generatorRun.Dispose();
                                generatorRun = null;
                            }

                            currentYinItemSystem += GetCachedOrEstimatedHeight(treeView, childHierarchyLevel);
                        }
                        //Debug(treeViewItem, "Current y for " + i + ": " + currentYinItemSystem);
                    }
                }
                finally
                {
                    //just for safety
                    if (generatorRun != null)
                    {
                        generatorRun.Dispose();
                        generatorRun = null;
                    }
                }

                //Debug("Cleaning all items but " + firstVisibleItemIndex + " to " + lastVisibleItemIndex + " for element " + itemsControl.DataContext);
                CleanUpItems(firstVisibleItemIndex, lastVisibleItemIndex);
            }
            else
            {
                //Debug("Virtualization is OFF.");
                GeneratorPosition startPos = generator.GeneratorPositionFromIndex(0);
                using (generator.StartAt(startPos, GeneratorDirection.Forward, true))
                {
                    for (int i = (startPos.Offset == 0) ? startPos.Index : startPos.Index + 1; i < itemsControl.Items.Count; i++)
                    {
                        // Get or create the child
                        bool newlyRealized;
                        TreeViewExItem child = generator.GenerateNext(out newlyRealized) as TreeViewExItem;
                        if (newlyRealized)
                        {
                            // Figure out if we need to insert the child at the end or somewhere in the middle
                            AddOrInsertItemToInternalChildren(i, child);
                            child.ParentTreeView = treeView ?? treeViewItem.ParentTreeView;
                            generator.PrepareItemContainer(child);
                        }

                        child.Measure(new Size(double.MaxValue, double.MaxValue));
                        // now get the real height
                        double height = child.DesiredSize.Height;
                        // add real height to current position
                        currentYinItemSystem += height;
                        // save the maximum needed width
                        maxWidth = Math.Max(maxWidth, child.DesiredSize.Width);
                    }
                }
            }

            if (double.IsPositiveInfinity(maxWidth) || double.IsPositiveInfinity(currentYinItemSystem))
            {
                throw new InvalidOperationException("???");
            }

            Extent = new Size(maxWidth, currentYinItemSystem);
            Viewport = availableSize;
            Debug(treeViewItem, "Desired height: " + Extent.Height);
            return Extent;
        }
Example #49
0
        public SelectionMultiple(TreeViewEx treeViewEx)
        {
            this.treeViewEx = treeViewEx;

            BorderSelectionLogic = new BorderSelectionLogic(treeViewEx, TreeViewElementFinder.FindAll(treeViewEx, false));
        }
Example #50
0
		public static void Focus(TreeViewEx element)
		{
			//System.Diagnostics.Debug.WriteLine("Focus Tree with helper");
			FocusCore(element);
		}