Inheritance: System.Windows.Controls.TreeViewItem
Esempio n. 1
0
        private IEnumerable <ExTreeViewItem> GetItemsBetween(ExTreeViewItem startItem, ExTreeViewItem endItem)
        {
            var allVisibleItems = GetAllVisibleItems(this).ToList();
            var startIndex      = allVisibleItems.IndexOf(startItem);
            var endIndex        = allVisibleItems.IndexOf(endItem);

            if (endIndex == startIndex)
            {
                return new List <ExTreeViewItem> {
                           endItem
                }
            }
            ;
            var filteredItems = new List <ExTreeViewItem>();

            if (endIndex > startIndex)
            {
                for (int i = startIndex; i <= endIndex; i++)
                {
                    filteredItems.Add(allVisibleItems[i]);
                }
            }
            else
            {
                for (int i = startIndex; i >= endIndex; i--)
                {
                    filteredItems.Add(allVisibleItems[i]);
                }
            }
            return(filteredItems);
        }
Esempio n. 2
0
        private bool TraverseTree(Func <object, ExTreeViewItem, bool> action, ExTreeViewItem parent = null)
        {
            ItemContainerGenerator g     = ItemContainerGenerator;
            IEnumerable            items = Items;

            if (parent != null)
            {
                items = parent.Items;
                g     = parent.ItemContainerGenerator;
            }

            foreach (object item in items)
            {
                var treeItem = (ExTreeViewItem)g.ContainerFromItem(item);
                if (treeItem == null)
                {
                    continue;
                }

                if (!action(item, treeItem))
                {
                    return(false);
                }

                if (!TraverseTree(action, treeItem))
                {
                    return(false);
                }
            }

            return(true);
        }
Esempio n. 3
0
		public TreeViewDropAdorner (ExTreeViewItem item, RowDropPosition position)
			: base (item)
		{
			IsHitTestVisible = false;
			
			double indent = ((item.Level + 1) * LevelToIndentConverter.IndentSize) + 5;

			this.shape = new System.Windows.Shapes.Rectangle();
			this.shape.Stroke = Brushes.DimGray;
			this.shape.Width = item.ActualWidth - indent;

			switch (position) {
				case RowDropPosition.Into:
					this.shape.Height = item.ActualHeight;
					this.shape.Margin = new Thickness (indent, 0, 0, 0);
					this.shape.StrokeDashArray = new DoubleCollection { 1, 0, 1 };
				break;

				case RowDropPosition.Before:
					this.shape.Height = 1;
					this.shape.Margin = new Thickness (indent, 0, 0, 0);
				break;

				case RowDropPosition.After:
					this.shape.Height = 1;
					this.shape.Margin = new Thickness (indent, item.ActualHeight, 0, 0);
				break;
			}
		}
        public TreeViewDropAdorner(ExTreeViewItem item, RowDropPosition position)
            : base(item)
        {
            IsHitTestVisible = false;

            double indent = ((item.Level + 1) * LevelToIndentConverter.IndentSize) + 5;

            this.shape        = new System.Windows.Shapes.Rectangle();
            this.shape.Stroke = Brushes.DimGray;
            this.shape.Width  = item.ActualWidth - indent;

            switch (position)
            {
            case RowDropPosition.Into:
                this.shape.Height          = item.ActualHeight;
                this.shape.Margin          = new Thickness(indent, 0, 0, 0);
                this.shape.StrokeDashArray = new DoubleCollection {
                    1, 0, 1
                };
                break;

            case RowDropPosition.Before:
                this.shape.Height = 1;
                this.shape.Margin = new Thickness(indent, 0, 0, 0);
                break;

            case RowDropPosition.After:
                this.shape.Height = 1;
                this.shape.Margin = new Thickness(indent, item.ActualHeight, 0, 0);
                break;
            }
        }
Esempio n. 5
0
        private ExTreeViewItem GetVisibleTreeItem(TreePosition pos, Action <ExTreeViewItem> walk = null)
        {
            Tree.UpdateLayout();
            Stack <TreeStoreNode> nodes = new Stack <TreeStoreNode> ();

            TreeStoreNode node = (TreeStoreNode)pos;

            do
            {
                nodes.Push(node);
                node = node.Parent;
            } while (node != null);

            ExTreeViewItem         treeItem = null;
            ItemContainerGenerator g        = Tree.ItemContainerGenerator;

            while (nodes.Count > 0)
            {
                node     = nodes.Pop();
                treeItem = (ExTreeViewItem)g.ContainerFromItem(node);
                treeItem.UpdateLayout();
                g = treeItem.ItemContainerGenerator;

                if (walk != null)
                {
                    walk(treeItem);
                }
            }

            return(treeItem);
        }
Esempio n. 6
0
 private void FocusItem(ExTreeViewItem exTreeViewItem)
 {
     if (exTreeViewItem != null)
     {
         focusedTreeViewItem = exTreeViewItem;
         exTreeViewItem.Focus();
     }
 }
Esempio n. 7
0
        public void ScrollToRow(TreePosition pos)
        {
            ExTreeViewItem item = GetVisibleTreeItem(pos);

            if (item != null)
            {
                item.BringIntoView();
            }
        }
Esempio n. 8
0
        public void SelectItem(ExTreeViewItem item)
        {
            if (item == null)
            {
                return;
            }

            FocusItem(item);
            if (!CtrlPressed)
            {
                SelectedItems.Clear();
            }
            if (ShiftPressed)
            {
                if (shiftStart == null)
                {
                    shiftStart = item;
                }
                if (shiftEnd != null)                //Erase previous selection of shift
                {
                    foreach (var forEachItem in GetItemsBetween(shiftStart, shiftEnd))
                    {
                        SelectedItems.Remove(forEachItem.DataContext);
                    }
                }
                shiftEnd = item;
                if (this.SelectionMode == SWC.SelectionMode.Single)
                {
                    SelectedItems.Add(item.DataContext);
                }
                else
                {
                    foreach (var forEachItem in GetItemsBetween(shiftStart, shiftEnd))
                    {
                        SelectedItems.Add(forEachItem.DataContext);
                    }
                }
            }
            else
            {
                shiftEnd   = null;
                shiftStart = item;
                if (CtrlPressed && SelectedItems.Contains(item.DataContext))
                {
                    SelectedItems.Remove(item.DataContext);
                }
                else
                {
                    SelectedItems.Add(item.DataContext);
                }
            }
        }
Esempio n. 9
0
        private ExTreeViewItem FindSameLevelParent(ExTreeViewItem item, int parentLevel)
        {
            while (item.Level != parentLevel)
            {
                item = ItemsControlFromItemContainer(item) as ExTreeViewItem;
                if (item == null)
                {
                    return(null);
                }
            }

            return(item);
        }
Esempio n. 10
0
        public Rectangle GetCellBounds(TreePosition pos, CellView cell, bool includeMargin)
        {
            ExTreeViewItem item = GetVisibleTreeItem(pos);

            if (item == null)
            {
                return(Rectangle.Zero);
            }

            // this works only if the wpf layout remains the same
            try {
                var stackpanel   = VisualTreeHelper.GetChild(item, 0);
                var border       = VisualTreeHelper.GetChild(stackpanel, 0);
                var rowpresenter = VisualTreeHelper.GetChild(border, 0) as FrameworkElement;

                if (VisualTreeHelper.GetChildrenCount(rowpresenter) < cellViews [cell].ColumnIndex)
                {
                    return(Rectangle.Zero);
                }

                var colpresenter = VisualTreeHelper.GetChild(rowpresenter, cellViews [cell].ColumnIndex) as FrameworkElement;
                var colchild     = VisualTreeHelper.GetChild(colpresenter, 0) as FrameworkElement;

                if (cellViews [cell].Column.Views.Count > 1 && colchild is System.Windows.Controls.StackPanel)
                {
                    var childStack = colchild as System.Windows.Controls.StackPanel;
                    if (childStack == null || VisualTreeHelper.GetChildrenCount(childStack) < cellViews [cell].CellIndex)
                    {
                        return(Rectangle.Zero);
                    }
                    var cellpresenter = VisualTreeHelper.GetChild(childStack, cellViews [cell].ColumnIndex == 0 ? cellViews [cell].CellIndex + 1 : cellViews [cell].CellIndex) as FrameworkElement;
                    var position      = cellpresenter.TransformToAncestor(Tree).Transform(new System.Windows.Point(-Tree.Padding.Left, 0));
                    var rect          = new Rect(position, cellpresenter.RenderSize);
                    return(rect.ToXwtRect());
                }
                else
                {
                    if (cellViews [cell].ColumnIndex == 0)
                    {
                        colchild = VisualTreeHelper.GetChild(colchild, 1) as FrameworkElement;
                    }
                    var position = colchild.TransformToAncestor(Tree).Transform(new System.Windows.Point(-Tree.Padding.Left, 0));
                    var rect     = new Rect(position, colchild.RenderSize);
                    return(rect.ToXwtRect());
                }
            } catch (ArgumentOutOfRangeException) {
                return(Rectangle.Zero);
            } catch (ArgumentNullException) {
                return(Rectangle.Zero);
            }
        }
Esempio n. 11
0
        private ExTreeViewItem GetPrevItem(ExTreeViewItem p)
        {
            var items    = GetAllVisibleItems(this).ToList();
            int indexOfP = items.IndexOf(p);

            if (indexOfP == 0)
            {
                return(p);
            }
            else
            {
                return(items[indexOfP - 1]);
            }
        }
Esempio n. 12
0
        private ExTreeViewItem GetNextItem(ExTreeViewItem p)
        {
            var items    = GetAllVisibleItems(this).ToList();
            int indexOfP = items.IndexOf(p);

            if (indexOfP + 1 == items.Count)
            {
                return(p);
            }
            else
            {
                return(items[indexOfP + 1]);
            }
        }
Esempio n. 13
0
        public bool GetDropTargetRow(double x, double y, out RowDropPosition pos, out TreePosition nodePosition)
        {
            nodePosition      = null;
            this.dropPosition = pos = RowDropPosition.Into;

            x *= WidthPixelRatio;
            y *= HeightPixelRatio;

            var result = VisualTreeHelper.HitTest(Tree, new System.Windows.Point(x, y)) as PointHitTestResult;

            var element = (result != null) ? result.VisualHit as FrameworkElement : null;

            while (element != null)
            {
                if (element is ExTreeViewItem)
                {
                    break;
                }
                if (element is ExTreeView)                 // We can't succeed past this point
                {
                    return(false);
                }

                element = VisualTreeHelper.GetParent(element) as FrameworkElement;
            }

            this.dropTarget = (ExTreeViewItem)element;

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

            var point = ((UIElement)result.VisualHit).TranslatePoint(result.PointHit, element);

            double edge = element.ActualHeight * 0.15;

            if (point.Y <= edge)
            {
                this.dropPosition = pos = RowDropPosition.Before;
            }
            else if (point.Y >= element.ActualHeight - edge)
            {
                this.dropPosition = pos = RowDropPosition.After;
            }

            nodePosition = element.DataContext as TreeStoreNode;
            return(true);
        }
Esempio n. 14
0
        public ExTreeViewItem ContainerFromObject(object item)
        {
            ExTreeViewItem treeItem = null;

            TraverseTree((o, i) => {
                if (o == item)
                {
                    treeItem = i;
                    return(false);
                }

                return(true);
            });

            return(treeItem);
        }
Esempio n. 15
0
        private bool TraverseTree(Func <object, ExTreeViewItem, bool> action, ExTreeViewItem parent = null)
        {
            ItemContainerGenerator g     = ItemContainerGenerator;
            IEnumerable            items = Items;

            if (parent != null)
            {
                items = parent.Items;
                g     = parent.ItemContainerGenerator;
            }
            else
            {
                UpdateLayout();
            }

            foreach (object item in items)
            {
                var treeItem = (ExTreeViewItem)g.ContainerFromItem(item);
                if (treeItem == null)
                {
                    var ig = (IItemContainerGenerator)g;
                    using (ig.StartAt(new GeneratorPosition(-1, 1), GeneratorDirection.Forward)) {
                        ig.GenerateNext();
                    }
                    treeItem = (ExTreeViewItem)g.ContainerFromItem(item);
                    if (treeItem == null)
                    {
                        continue;
                    }
                }

                if (!action(item, treeItem))
                {
                    return(false);
                }

                if (!TraverseTree(action, treeItem))
                {
                    return(false);
                }
            }

            return(true);
        }
Esempio n. 16
0
        private IEnumerable <ExTreeViewItem> GetAllVisibleItems(ItemsControl itemsControl)
        {
            foreach (var item in itemsControl.Items)
            {
                ExTreeViewItem treeViewItem = (ExTreeViewItem)itemsControl.ItemContainerGenerator.ContainerFromItem(item);
                if (treeViewItem == null)
                {
                    continue;
                }
                if (treeViewItem.IsVisible && treeViewItem.Visibility == Visibility.Visible)
                {
                    yield return(treeViewItem);

                    if (treeViewItem.IsExpanded)
                    {
                        foreach (var childItem in GetAllVisibleItems(treeViewItem))
                        {
                            yield return(childItem);
                        }
                    }
                }
            }
        }
Esempio n. 17
0
		private ExTreeViewItem GetNextItem(ExTreeViewItem p)
		{
			var items = GetAllVisibleItems(this).ToList();
			int indexOfP = items.IndexOf(p);
			if (indexOfP + 1 == items.Count)
				return p;
			else
				return items[indexOfP + 1];
		}
Esempio n. 18
0
		private ExTreeViewItem GetPrevItem(ExTreeViewItem p)
		{
			var items = GetAllVisibleItems(this).ToList();
			int indexOfP = items.IndexOf(p);
			if (indexOfP == 0)
				return p;
			else
				return items[indexOfP - 1];
		}
Esempio n. 19
0
		private void FocusItem(ExTreeViewItem exTreeViewItem)
		{
			if (exTreeViewItem != null)
			{
				focusedTreeViewItem = exTreeViewItem;
				exTreeViewItem.Focus();
			}
		}
Esempio n. 20
0
		private IEnumerable<ExTreeViewItem> GetItemsBetween(ExTreeViewItem startItem, ExTreeViewItem endItem)
		{
			var allVisibleItems = GetAllVisibleItems(this).ToList();
			var startIndex = allVisibleItems.IndexOf(startItem);
			var endIndex = allVisibleItems.IndexOf(endItem);

			if (endIndex == startIndex)
				return new List<ExTreeViewItem> { endItem };
			var filteredItems = new List<ExTreeViewItem>();
			if (endIndex > startIndex)
				for (int i = startIndex; i <= endIndex; i++)
					filteredItems.Add(allVisibleItems[i]);
			else
				for (int i = startIndex; i >= endIndex; i--)
					filteredItems.Add(allVisibleItems[i]);
			return filteredItems;
		}
Esempio n. 21
0
		public void SelectItem(ExTreeViewItem item)
		{
			FocusItem(item);
			if (!CtrlPressed)
				SelectedItems.Clear();
			if (ShiftPressed)
			{
				if (shiftEnd != null)//Erase previous selection of shift
					foreach (var forEachItem in GetItemsBetween(shiftStart, shiftEnd))
						SelectedItems.Remove(forEachItem.DataContext);
				shiftEnd = item;
				foreach (var forEachItem in GetItemsBetween(shiftStart, shiftEnd))
					SelectedItems.Add(forEachItem.DataContext);
			}
			else
			{
				shiftEnd = null;
				shiftStart = item;
				if (CtrlPressed && SelectedItems.Contains(item.DataContext))
					SelectedItems.Remove(item.DataContext);
				else
					SelectedItems.Add(item.DataContext);
			}
		}
Esempio n. 22
0
		private bool TraverseTree (Func<object, ExTreeViewItem, bool> action, ExTreeViewItem parent = null)
		{
			ItemContainerGenerator g = ItemContainerGenerator;
			IEnumerable items = Items;
			if (parent != null) {
				items = parent.Items;
				g = parent.ItemContainerGenerator;
			}

			foreach (object item in items) {
				var treeItem = (ExTreeViewItem) g.ContainerFromItem (item);
				if (treeItem == null)
					continue;

				if (!action (item, treeItem))
					return false;
				
				if (!TraverseTree (action, treeItem))
					return false;
			}

			return true;
		}
Esempio n. 23
0
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            bool add = true;

            if (TreeView.SelectedItems.Count > 0 && this.view.SelectionMode == SWC.SelectionMode.Extended)
            {
                if (!(Keyboard.IsKeyDown(WKey.LeftCtrl) || Keyboard.IsKeyDown(WKey.RightCtrl)))
                {
                    if (Keyboard.IsKeyDown(WKey.LeftShift))
                    {
                        var lastSelected = TreeView.ContainerFromObject(TreeView.SelectedItems [TreeView.SelectedItems.Count - 1]);

                        TreeView.SelectedItems.Clear();

                        ExTreeViewItem top, bottom;
                        if (Level == lastSelected.Level)
                        {
                            top    = this;
                            bottom = lastSelected;
                        }
                        else
                        {
                            top    = (Level < lastSelected.Level) ? this : lastSelected;
                            bottom = (Level > lastSelected.Level) ? this : lastSelected;
                        }

                        var itemControl = ItemsControlFromItemContainer(top);
                        var items       = itemControl.Items;
                        var g           = itemControl.ItemContainerGenerator;

                        ExTreeViewItem currentTop    = top;
                        ExTreeViewItem currentBottom = FindSameLevelParent(bottom, top.Level);

                        bool adding = false;
                        for (int i = 0; i < items.Count; ++i)
                        {
                            object       item      = items [i];
                            TreeViewItem container = (TreeViewItem)g.ContainerFromItem(item);

                            bool endpoint = (container == currentTop || container == currentBottom);

                            if (endpoint)
                            {
                                if (currentTop != currentBottom)
                                {
                                    adding = !adding;
                                }
                            }
                            else if (!adding)
                            {
                                continue;
                            }

                            TreeView.SelectedItems.Add(item);

                            if (endpoint && !adding)
                            {
                                if (bottom != currentBottom)
                                {
                                    i = -1;

                                    items         = currentBottom.Items;
                                    g             = currentBottom.ItemContainerGenerator;
                                    currentTop    = (ExTreeViewItem)g.ContainerFromItem(items [0]);
                                    currentBottom = FindSameLevelParent(bottom, currentBottom.Level + 1);
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        TreeView.SelectedItems.Clear();
                    }
                }
                else if (IsSelected)
                {
                    add = false;
                }
            }

            if (!add || !IsSelected)
            {
                if (add)
                {
                    TreeView.SelectedItems.Add(DataContext);
                }
                else
                {
                    TreeView.SelectedItems.Remove(DataContext);
                }

                e.Handled = true;
            }

            base.OnMouseLeftButtonDown(e);
        }
Esempio n. 24
0
        public bool GetDropTargetRow(double x, double y, out RowDropPosition pos, out TreePosition nodePosition)
        {
            nodePosition = null;
            this.dropPosition = pos = RowDropPosition.Into;

            var result = VisualTreeHelper.HitTest (Tree, new System.Windows.Point (x, y)) as PointHitTestResult;

            var element = (result != null) ? result.VisualHit as FrameworkElement : null;
            while (element != null) {
                if (element is ExTreeViewItem)
                    break;
                if (element is ExTreeView) // We can't succeed past this point
                    return false;

                element = VisualTreeHelper.GetParent (element) as FrameworkElement;
            }

            this.dropTarget = (ExTreeViewItem)element;

            if (element == null)
                return false;

            var point = ((UIElement)result.VisualHit).TranslatePoint (result.PointHit, element);

            double edge = element.ActualHeight * 0.15;
            if (point.Y <= edge)
                this.dropPosition = pos = RowDropPosition.Before;
            else if (point.Y >= element.ActualHeight - edge)
                this.dropPosition = pos = RowDropPosition.After;

            nodePosition = element.DataContext as TreeStoreNode;
            return true;
        }
Esempio n. 25
0
        public Rectangle GetRowBounds(TreePosition pos, bool includeMargin)
        {
            ExTreeViewItem item = GetVisibleTreeItem(pos);

            if (item == null)
            {
                return(Rectangle.Zero);
            }

            // this works only if the wpf layout remains the same
            try {
                var stackpanel = VisualTreeHelper.GetChild(item, 0);
                var border     = VisualTreeHelper.GetChild(stackpanel, 0) as FrameworkElement;

                var rect = Rectangle.Zero;
                if (includeMargin)
                {
                    var position = border.TransformToAncestor(Tree).Transform(new System.Windows.Point(0, 0));
                    rect    = new Rect(position, border.RenderSize).ToXwtRect();
                    rect.X -= Tree.Padding.Left;
                }
                else
                {
                    var rowpresenter = VisualTreeHelper.GetChild(border, 0) as FrameworkElement;
                    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(rowpresenter); i++)
                    {
                        var colpresenter = VisualTreeHelper.GetChild(rowpresenter, i) as FrameworkElement;
                        var colchild     = VisualTreeHelper.GetChild(colpresenter, 0) as FrameworkElement;
                        var cellcount    = VisualTreeHelper.GetChildrenCount(colchild);
                        if (cellcount > 1)
                        {
                            for (int j = 0; j < cellcount; j++)
                            {
                                var cell      = VisualTreeHelper.GetChild(colchild, j) as FrameworkElement;
                                var position  = cell.TransformToAncestor(Tree).Transform(new System.Windows.Point(-Tree.Padding.Left, 0));
                                var cell_rect = new Rect(position, cell.RenderSize).ToXwtRect();
                                if (rect == Rectangle.Zero)
                                {
                                    rect = cell_rect;
                                }
                                else
                                {
                                    rect = rect.Union(cell_rect);
                                }
                            }
                        }
                        else
                        {
                            var position  = colchild.TransformToAncestor(Tree).Transform(new System.Windows.Point(-Tree.Padding.Left, 0));
                            var cell_rect = new Rect(position, colchild.RenderSize).ToXwtRect();
                            if (rect == Rectangle.Zero)
                            {
                                rect = cell_rect;
                            }
                            else
                            {
                                rect = rect.Union(cell_rect);
                            }
                        }
                    }
                    //var position = rowpresenter.TransformToAncestor (Tree).Transform(new System.Windows.Point(0,0));
                    //rect = new Rect (position, rowpresenter.RenderSize).ToXwtRect();
                    //rect.X -= Tree.Padding.Left + Tree.BorderThickness.Left;
                }

                return(rect);
            } catch (ArgumentOutOfRangeException) {
                return(Rectangle.Zero);
            } catch (ArgumentNullException) {
                return(Rectangle.Zero);
            }
        }
Esempio n. 26
0
        private bool TraverseTree(Func<object, ExTreeViewItem, bool> action, ExTreeViewItem parent = null)
        {
            ItemContainerGenerator g = ItemContainerGenerator;
            IEnumerable items = Items;
            if (parent != null) {
                items = parent.Items;
                g = parent.ItemContainerGenerator;
            }

            foreach (object item in items) {
                var treeItem = (ExTreeViewItem)g.ContainerFromItem (item);
                if (treeItem == null) {
                    var ig = (IItemContainerGenerator)g;
                    using (ig.StartAt (new GeneratorPosition (-1, 1), GeneratorDirection.Forward)) {
                        ig.GenerateNext ();
                    }
                    treeItem = (ExTreeViewItem)g.ContainerFromItem (item);
                    if (treeItem == null)
                        continue;
                }

                if (!action (item, treeItem))
                    return false;

                if (!TraverseTree (action, treeItem))
                    return false;
            }

            return true;
        }
Esempio n. 27
0
 public ExTreeViewItemAutomationPeer(ExTreeViewItem owner) : base(owner)
 {
 }
Esempio n. 28
0
        private ExTreeViewItem FindSameLevelParent(ExTreeViewItem item, int parentLevel)
        {
            while (item.Level != parentLevel) {
                item = ItemsControlFromItemContainer (item) as ExTreeViewItem;
                if (item == null)
                    return null;
            }

            return item;
        }