private void TheTreeView_PreviewSelectionChanged(object sender, PreviewSelectionChangedEventArgs e)
		{
			if (LockSelectionCheck.IsChecked == true)
			{
				// The current selection is locked by user request (Lock CheckBox is checked).
				// Don't allow any changes to the selection at all.
				e.CancelThis = true;
			}
			else
			{
				// Selection is not locked, apply other conditions.
				// Require all selected items to be of the same type. If an item of another data
				// type is already selected, don't include this new item in the selection.
				if (e.Selecting && TheTreeView.SelectedItems.Count > 0)
				{
					e.CancelThis = e.Item.GetType() != TheTreeView.SelectedItems[0].GetType();
				}
			}

			//if (e.Selecting)
			//{
			//    System.Diagnostics.Debug.WriteLine("Preview: Selecting " + e.Item + (e.Cancel ? " - cancelled" : ""));
			//}
			//else
			//{
			//    System.Diagnostics.Debug.WriteLine("Preview: Deselecting " + e.Item + (e.Cancel ? " - cancelled" : ""));
			//}
		}
Beispiel #2
0
        public bool SelectCore(MultiSelectTreeViewItem item)
        {
            if (treeView.SelectedItems.Count > 0)
            {
                foreach (var selItem in new ArrayList(treeView.SelectedItems))
                {
                    var e2 = new PreviewSelectionChangedEventArgs(false, selItem);
                    OnPreviewSelectionChanged(e2);
                    if (e2.CancelAll)
                    {
                        FocusHelper.Focus(item);
                        return(false);
                    }
                    if (!e2.CancelThis)
                    {
                        treeView.SelectedItems.Remove(selItem);
                    }
                }
            }

            var e = new PreviewSelectionChangedEventArgs(true, item.DataContext);

            OnPreviewSelectionChanged(e);
            if (e.CancelAny)
            {
                FocusHelper.Focus(item, true);
                return(false);
            }

            treeView.SelectedItems.Add(item.DataContext);
            FocusHelper.Focus(item, true);
            return(true);
        }
        internal bool DeselectRecursive(MultiSelectTreeViewItem item, bool includeSelf)
        {
            List <MultiSelectTreeViewItem> selectedChildren = new List <MultiSelectTreeViewItem>();

            if (includeSelf)
            {
                if (item.IsSelected)
                {
                    var e = new PreviewSelectionChangedEventArgs(false, item.DataContext);
                    OnPreviewSelectionChanged(e);
                    if (e.CancelAny)
                    {
                        return(false);
                    }
                    selectedChildren.Add(item);
                }
            }
            if (!CollectDeselectRecursive(item, selectedChildren))
            {
                return(false);
            }
            foreach (var child in selectedChildren)
            {
                child.IsSelected = false;
            }
            return(true);
        }
 private bool CollectDeselectRecursive(MultiSelectTreeViewItem item, List <MultiSelectTreeViewItem> selectedChildren)
 {
     foreach (var child in item.Items)
     {
         MultiSelectTreeViewItem tvi = item.ItemContainerGenerator.ContainerFromItem(child) as MultiSelectTreeViewItem;
         if (tvi != null)
         {
             if (tvi.IsSelected)
             {
                 var e = new PreviewSelectionChangedEventArgs(false, child);
                 OnPreviewSelectionChanged(e);
                 if (e.CancelAny)
                 {
                     return(false);
                 }
                 selectedChildren.Add(tvi);
             }
             if (!CollectDeselectRecursive(tvi, selectedChildren))
             {
                 return(false);
             }
         }
     }
     return(true);
 }
Beispiel #5
0
		protected void OnPreviewSelectionChanged(PreviewSelectionChangedEventArgs e)
		{
			var handler = PreviewSelectionChanged;
			if (handler != null)
			{
				handler(this, e);
			}
		}
Beispiel #6
0
		internal bool ClearSelectionByRectangle()
		{
			foreach (var item in new ArrayList(SelectedItems))
			{
				var e = new PreviewSelectionChangedEventArgs(false, item);
				OnPreviewSelectionChanged(e);
				if (e.CancelAny) return false;
			}
			
			SelectedItems.Clear();
			return true;
		}
Beispiel #7
0
        public bool Deselect(MultiSelectTreeViewItem item, bool bringIntoView = false)
        {
            var e = new PreviewSelectionChangedEventArgs(false, item.DataContext);

            OnPreviewSelectionChanged(e);
            if (e.CancelAny)
            {
                return(false);
            }

            treeView.SelectedItems.Remove(item.DataContext);
            FocusHelper.Focus(item, bringIntoView);
            return(true);
        }
        public bool Deselect(MultiSelectTreeViewItem item, bool bringIntoView = false)
        {
            var e = new PreviewSelectionChangedEventArgs(false, item.DataContext);
            OnPreviewSelectionChanged(e);
            if (e.CancelAny) return false;

            treeView.SelectedItems.Remove(item.DataContext);
            if (item.DataContext == lastShiftRoot)
            {
                lastShiftRoot = null;
            }
            FocusHelper.Focus(item, bringIntoView);
            return true;
        }
        internal bool SelectByRectangle(MultiSelectTreeViewItem item)
        {
            var e = new PreviewSelectionChangedEventArgs(true, item.DataContext);

            OnPreviewSelectionChanged(e);
            if (e.CancelAny)
            {
                lastShiftRoot = item.DataContext;
                return(false);
            }

            if (!treeView.SelectedItems.Contains(item.DataContext))
            {
                treeView.SelectedItems.Add(item.DataContext);
            }
            lastShiftRoot = item.DataContext;
            return(true);
        }
        internal bool DeselectByRectangle(MultiSelectTreeViewItem item)
        {
            var e = new PreviewSelectionChangedEventArgs(false, item.DataContext);

            OnPreviewSelectionChanged(e);
            if (e.CancelAny)
            {
                lastShiftRoot = item.DataContext;
                return(false);
            }

            treeView.SelectedItems.Remove(item.DataContext);
            if (item.DataContext == lastShiftRoot)
            {
                lastShiftRoot = null;
            }
            return(true);
        }
Beispiel #11
0
		public bool ClearSelection()
		{
			if (SelectedItems.Count > 0)
			{
				foreach (var selItem in SelectedItems)
				{
					var e = new PreviewSelectionChangedEventArgs(false, selItem);
					OnPreviewSelectionChanged(e);
					if (e.CancelAny)
					{
						return false;
					}
				}

				SelectedItems.Clear();
			}
			return true;
		}
        public bool ClearSelection()
        {
            if (SelectedItems.Count > 0)
            {
                // Make a copy of the list and ignore changes to the selection while raising events
                foreach (var selItem in new ArrayList(SelectedItems))
                {
                    var e = new PreviewSelectionChangedEventArgs(false, selItem);
                    OnPreviewSelectionChanged(e);
                    if (e.CancelAny)
                    {
                        return(false);
                    }
                }

                SelectedItems.Clear();
            }
            return(true);
        }
        public bool SelectAllFromKey()
        {
            var items = MultiSelectTreeView.RecursiveTreeViewItemEnumerable(treeView, false, false).ToList();

            // Add new selected items
            foreach (var item in items.Where(i => !treeView.SelectedItems.Contains(i.DataContext)))
            {
                var e = new PreviewSelectionChangedEventArgs(true, item.DataContext);
                OnPreviewSelectionChanged(e);
                if (e.CancelAll)
                {
                    return(false);
                }
                if (!e.CancelThis)
                {
                    treeView.SelectedItems.Add(item.DataContext);
                }
            }
            return(true);
        }
Beispiel #14
0
        public bool SelectCurrentBySpace()
        {
            var item = GetFocusedItem();
            var e    = new PreviewSelectionChangedEventArgs(true, item.DataContext);

            OnPreviewSelectionChanged(e);
            if (e.CancelAny)
            {
                FocusHelper.Focus(item, true);
                return(false);
            }

            item.IsSelected = true;
            if (!treeView.SelectedItems.Contains(item.DataContext))
            {
                treeView.SelectedItems.Add(item.DataContext);
            }

            FocusHelper.Focus(item, true);
            return(true);
        }
        public bool SelectCurrentBySpace()
        {
            // Another item was focused by Ctrl+Arrow key
            var item = GetFocusedItem();

            if (treeView.SelectedItems.Contains(item.DataContext))
            {
                // With Ctrl key, toggle this item selection (deselect now).
                // Without Ctrl key, always select it (is already selected).
                if (IsControlKeyDown)
                {
                    if (!Deselect(item, true))
                    {
                        return(false);
                    }
                    item.IsSelected = false;
                }
            }
            else
            {
                var e = new PreviewSelectionChangedEventArgs(true, item.DataContext);
                OnPreviewSelectionChanged(e);
                if (e.CancelAny)
                {
                    FocusHelper.Focus(item, true);
                    return(false);
                }

                item.IsSelected = true;
                if (!treeView.SelectedItems.Contains(item.DataContext))
                {
                    treeView.SelectedItems.Add(item.DataContext);
                }
            }
            FocusHelper.Focus(item, true);
            return(true);
        }
        public bool Select(MultiSelectTreeViewItem item)
        {
            if (IsControlKeyDown)
            {
                if (treeView.SelectedItems.Contains(item.DataContext))
                {
                    return(Deselect(item, true));
                }
                else
                {
                    var e = new PreviewSelectionChangedEventArgs(true, item.DataContext);
                    OnPreviewSelectionChanged(e);
                    if (e.CancelAny)
                    {
                        FocusHelper.Focus(item, true);
                        return(false);
                    }

                    return(SelectCore(item));
                }
            }
            else
            {
                if (treeView.SelectedItems.Count == 1 &&
                    treeView.SelectedItems[0] == item.DataContext)
                {
                    // Requested to select the single already-selected item. Don't change the selection.
                    FocusHelper.Focus(item, true);
                    lastShiftRoot = item.DataContext;
                    return(true);
                }
                else
                {
                    return(SelectCore(item));
                }
            }
        }
		public bool Select(MultiSelectTreeViewItem item)
		{
			if (IsControlKeyDown)
			{
				if (treeView.SelectedItems.Contains(item.DataContext))
				{
					return Deselect(item, true);
				}
				else
				{
					var e = new PreviewSelectionChangedEventArgs(true, item.DataContext);
					OnPreviewSelectionChanged(e);
					if (e.CancelAny)
					{
						FocusHelper.Focus(item, true);
						return false;
					}

					return SelectCore(item);
				}
			}
			else
			{
				if (treeView.SelectedItems.Count == 1 &&
					treeView.SelectedItems[0] == item.DataContext)
				{
					// Requested to select the single already-selected item. Don't change the selection.
					FocusHelper.Focus(item, true);
					lastShiftRoot = item.DataContext;
					return true;
				}
				else
				{
					return SelectCore(item);
				}
			}
		}
		internal bool DeselectRecursive(MultiSelectTreeViewItem item, bool includeSelf)
		{
			List<MultiSelectTreeViewItem> selectedChildren = new List<MultiSelectTreeViewItem>();
			if (includeSelf)
			{
				if (item.IsSelected)
				{
					var e = new PreviewSelectionChangedEventArgs(false, item.DataContext);
					OnPreviewSelectionChanged(e);
					if (e.CancelAny)
					{
						return false;
					}
					selectedChildren.Add(item);
				}
			}
			if (!CollectDeselectRecursive(item, selectedChildren))
			{
				return false;
			}
			foreach (var child in selectedChildren)
			{
				child.IsSelected = false;
			}
			return true;
		}
        public bool SelectCore(MultiSelectTreeViewItem item)
        {
            if (IsControlKeyDown)
            {
                if (!treeView.SelectedItems.Contains(item.DataContext))
                {
                    treeView.SelectedItems.Add(item.DataContext);
                }
                lastShiftRoot = item.DataContext;
            }
            else if (IsShiftKeyDown && treeView.SelectedItems.Count > 0)
            {
                object firstSelectedItem = lastShiftRoot ?? treeView.SelectedItems.First();
                MultiSelectTreeViewItem shiftRootItem = treeView.GetTreeViewItemsFor(new List <object> {
                    firstSelectedItem
                }).First();

                var newSelection = treeView.GetNodesToSelectBetween(shiftRootItem, item).Select(n => n.DataContext).ToList();
                // Make a copy of the list because we're modifying it while enumerating it
                var selectedItems = new object[treeView.SelectedItems.Count];
                treeView.SelectedItems.CopyTo(selectedItems, 0);
                // Remove all items no longer selected
                foreach (var selItem in selectedItems.Where(i => !newSelection.Contains(i)))
                {
                    var e = new PreviewSelectionChangedEventArgs(false, selItem);
                    OnPreviewSelectionChanged(e);
                    if (e.CancelAll)
                    {
                        FocusHelper.Focus(item);
                        return(false);
                    }
                    if (!e.CancelThis)
                    {
                        treeView.SelectedItems.Remove(selItem);
                    }
                }
                // Add new selected items
                foreach (var newItem in newSelection.Where(i => !selectedItems.Contains(i)))
                {
                    var e = new PreviewSelectionChangedEventArgs(true, newItem);
                    OnPreviewSelectionChanged(e);
                    if (e.CancelAll)
                    {
                        FocusHelper.Focus(item, true);
                        return(false);
                    }
                    if (!e.CancelThis)
                    {
                        treeView.SelectedItems.Add(newItem);
                    }
                }
            }
            else
            {
                if (treeView.SelectedItems.Count > 0)
                {
                    foreach (var selItem in new ArrayList(treeView.SelectedItems))
                    {
                        var e2 = new PreviewSelectionChangedEventArgs(false, selItem);
                        OnPreviewSelectionChanged(e2);
                        if (e2.CancelAll)
                        {
                            FocusHelper.Focus(item);
                            lastShiftRoot = item.DataContext;
                            return(false);
                        }
                        if (!e2.CancelThis)
                        {
                            treeView.SelectedItems.Remove(selItem);
                        }
                    }
                }

                var e = new PreviewSelectionChangedEventArgs(true, item.DataContext);
                OnPreviewSelectionChanged(e);
                if (e.CancelAny)
                {
                    FocusHelper.Focus(item, true);
                    lastShiftRoot = item.DataContext;
                    return(false);
                }

                treeView.SelectedItems.Add(item.DataContext);
                lastShiftRoot = item.DataContext;
            }

            FocusHelper.Focus(item, true);
            return(true);
        }
Beispiel #20
0
        protected void OnPreviewSelectionChanged(PreviewSelectionChangedEventArgs e)
        {
            var handler = PreviewSelectionChanged;

            handler?.Invoke(this, e);
        }
Beispiel #21
0
 protected virtual void OnPreviewSelectionChanged(PreviewSelectionChangedEventArgs e)
 {
     e.RoutedEvent = PreviewSelectionChangedEvent;
     RaiseEvent(e);
 }
 protected void OnPreviewSelectionChanged(PreviewSelectionChangedEventArgs e)
 {
     var handler = PreviewSelectionChanged;
     if (handler != null)
     {
         handler(this, e);
         LastCancelAll = e.CancelAll;
     }
 }
        internal bool SelectByRectangle(MultiSelectTreeViewItem item)
        {
            var e = new PreviewSelectionChangedEventArgs(true, item.DataContext);
            OnPreviewSelectionChanged(e);
            if (e.CancelAny)
            {
                lastShiftRoot = item.DataContext;
                return false;
            }

            if (!treeView.SelectedItems.Contains(item.DataContext))
            {
                treeView.SelectedItems.Add(item.DataContext);
            }
            lastShiftRoot = item.DataContext;
            return true;
        }
        internal bool DeselectByRectangle(MultiSelectTreeViewItem item)
        {
            var e = new PreviewSelectionChangedEventArgs(false, item.DataContext);
            OnPreviewSelectionChanged(e);
            if (e.CancelAny)
            {
                lastShiftRoot = item.DataContext;
                return false;
            }

            treeView.SelectedItems.Remove(item.DataContext);
            if (item.DataContext == lastShiftRoot)
            {
                lastShiftRoot = null;
            }
            return true;
        }
        public bool SelectCurrentBySpace()
        {
            // Another item was focused by Ctrl+Arrow key
            var item = GetFocusedItem();
            if (treeView.SelectedItems.Contains(item.DataContext))
            {
                // With Ctrl key, toggle this item selection (deselect now).
                // Without Ctrl key, always select it (is already selected).
                if (IsControlKeyDown)
                {
                    if (!Deselect(item, true)) return false;
                    item.IsSelected = false;
                }
            }
            else
            {
                var e = new PreviewSelectionChangedEventArgs(true, item.DataContext);
                OnPreviewSelectionChanged(e);
                if (e.CancelAny)
                {
                    FocusHelper.Focus(item, true);
                    return false;
                }

                item.IsSelected = true;
                if (!treeView.SelectedItems.Contains(item.DataContext))
                {
                    treeView.SelectedItems.Add(item.DataContext);
                }
            }
            FocusHelper.Focus(item, true);
            return true;
        }
        internal bool ClearSelectionByRectangle()
        {
            foreach (var item in new ArrayList(SelectedItems))
            {
                var e = new PreviewSelectionChangedEventArgs(false, item);
                OnPreviewSelectionChanged(e);
                if (e.CancelAny) return false;
            }

            SelectedItems.Clear();
            return true;
        }
		private bool CollectDeselectRecursive(MultiSelectTreeViewItem item, List<MultiSelectTreeViewItem> selectedChildren)
		{
			foreach (var child in item.Items)
			{
				MultiSelectTreeViewItem tvi = item.ItemContainerGenerator.ContainerFromItem(child) as MultiSelectTreeViewItem;
				if (tvi != null)
				{
					if (tvi.IsSelected)
					{
						var e = new PreviewSelectionChangedEventArgs(false, child);
						OnPreviewSelectionChanged(e);
						if (e.CancelAny)
						{
							return false;
						}
						selectedChildren.Add(tvi);
					}
					if (!CollectDeselectRecursive(tvi, selectedChildren))
					{
						return false;
					}
				}
			}
			return true;
		}
Beispiel #28
0
 private void PreviewSelectionChangedHandler(object sender, PreviewSelectionChangedEventArgs e)
 {
     OnPreviewSelectionChanged(e);
 }
        public bool SelectCore(MultiSelectTreeViewItem item)
        {
            if (IsControlKeyDown)
            {
                if (!treeView.SelectedItems.Contains(item.DataContext))
                {
                    treeView.SelectedItems.Add(item.DataContext);
                }
                lastShiftRoot = item.DataContext;
            }
            else if (IsShiftKeyDown && treeView.SelectedItems.Count > 0)
            {
                object firstSelectedItem = lastShiftRoot ?? treeView.SelectedItems.First();
                MultiSelectTreeViewItem shiftRootItem = treeView.GetTreeViewItemsFor(new List<object> { firstSelectedItem }).First();

                var newSelection = treeView.GetNodesToSelectBetween(shiftRootItem, item).Select(n => n.DataContext).ToList();
                // Make a copy of the list because we're modifying it while enumerating it
                var selectedItems = new object[treeView.SelectedItems.Count];
                treeView.SelectedItems.CopyTo(selectedItems, 0);
                // Remove all items no longer selected
                foreach (var selItem in selectedItems.Where(i => !newSelection.Contains(i)))
                {
                    var e = new PreviewSelectionChangedEventArgs(false, selItem);
                    OnPreviewSelectionChanged(e);
                    if (e.CancelAll)
                    {
                        FocusHelper.Focus(item);
                        return false;
                    }
                    if (!e.CancelThis)
                    {
                        treeView.SelectedItems.Remove(selItem);
                    }
                }
                // Add new selected items
                foreach (var newItem in newSelection.Where(i => !selectedItems.Contains(i)))
                {
                    var e = new PreviewSelectionChangedEventArgs(true, newItem);
                    OnPreviewSelectionChanged(e);
                    if (e.CancelAll)
                    {
                        FocusHelper.Focus(item, true);
                        return false;
                    }
                    if (!e.CancelThis)
                    {
                        treeView.SelectedItems.Add(newItem);
                    }
                }
            }
            else
            {
                if (treeView.SelectedItems.Count > 0)
                {
                    foreach (var selItem in new ArrayList(treeView.SelectedItems))
                    {
                        var e2 = new PreviewSelectionChangedEventArgs(false, selItem);
                        OnPreviewSelectionChanged(e2);
                        if (e2.CancelAll)
                        {
                            FocusHelper.Focus(item);
                            lastShiftRoot = item.DataContext;
                            return false;
                        }
                        if (!e2.CancelThis)
                        {
                            treeView.SelectedItems.Remove(selItem);
                        }
                    }
                }

                var e = new PreviewSelectionChangedEventArgs(true, item.DataContext);
                OnPreviewSelectionChanged(e);
                if (e.CancelAny)
                {
                    FocusHelper.Focus(item, true);
                    lastShiftRoot = item.DataContext;
                    return false;
                }

                treeView.SelectedItems.Add(item.DataContext);
                lastShiftRoot = item.DataContext;
            }

            FocusHelper.Focus(item, true);
            return true;
        }
        public bool ClearSelection()
        {
            if (SelectedItems.Count > 0)
            {
                foreach (var selItem in SelectedItems)
                {
                    var e = new PreviewSelectionChangedEventArgs(false, selItem);
                    OnPreviewSelectionChanged(e);
                    if (e.CancelAny)
                    {
                        return false;
                    }
                }

                SelectedItems.Clear();
            }
            return true;
        }
 public bool SelectAllFromKey()
 {
     var items = MultiSelectTreeView.RecursiveTreeViewItemEnumerable(treeView, false, false).ToList();
     // Add new selected items
     foreach (var item in items.Where(i => !treeView.SelectedItems.Contains(i.DataContext)))
     {
         var e = new PreviewSelectionChangedEventArgs(true, item.DataContext);
         OnPreviewSelectionChanged(e);
         if (e.CancelAll)
         {
             return false;
         }
         if (!e.CancelThis)
         {
             treeView.SelectedItems.Add(item.DataContext);
         }
     }
     return true;
 }
		public bool ClearSelection()
		{
			if (SelectedItems.Count > 0)
			{
				// Make a copy of the list and ignore changes to the selection while raising events
				foreach (var selItem in new ArrayList(SelectedItems))
				{
					var e = new PreviewSelectionChangedEventArgs(false, selItem);
					OnPreviewSelectionChanged(e);
					if (e.CancelAny)
					{
						return false;
					}
				}

				SelectedItems.Clear();
			}
			return true;
		}