/// <summary> /// Moves the selected container to the container that is below the mouse (ignoreing the selected container) /// </summary> public void MoveSelectedToBelow() { if (SelectedContainer != null) { // get the new container of the selected container BaseContainer newContainer = FindContainerAtPoint(MousePosition, SelectedContainer); if (newContainer is PlaceHolder) { newContainer = newContainer.ParentContainer; if (newContainer is Container) { Container container = (Container)newContainer; if (container.RenderMode == ContainerRenderMode.Linear) { RemoveChild(SelectedContainer); PlaceHolder.ReplaceWith(SelectedContainer); SelectedContainer.Location = PlaceHolder.Location; RemovePlaceHolder(); return; } } } // update the location of the selected Point selectedScreen = SelectedContainer.PointToScreen(Point.Empty); Point difference = newContainer.PointToClient(selectedScreen); SelectedContainer.Location = difference; // move to new SelectedContainer.MoveTo(newContainer); RemovePlaceHolder(); } }
/// <summary> /// Selects the container that sends this event /// </summary> /// <param name="sender">container that sent the event</param> private void SelectContainer(Object sender) { if (SelectedContainer != null) { SelectedContainer.BackColor = Color.White; } SelectedContainer = (Container)sender; SelectedContainer.BackColor = Color.LightYellow; MoveSelectedToThis(); SelectedContainer.BringToFront(); OnSelectContainer?.Invoke(SelectedContainer); }
/// <summary> /// Provides handling for mouse button events. /// </summary> /// <returns>A value indicating whether the event was handled.</returns> internal bool HandleMouseButtonDown() { if (SelectedContainer != null) { if (SelectedContainer != FocusManager.GetFocusedElement()) { SelectedContainer.Focus(); } return(true); } return(false); }
/// <summary> /// Moves the selected container to this plan /// </summary> public void MoveSelectedToThis() { if (SelectedContainer != null) { if (SelectedContainer.ParentContainer is Container && ((Container)SelectedContainer.ParentContainer).RenderMode == ContainerRenderMode.Linear) { PutPlaceHolder(SelectedContainer); SelectedContainer.Location = PointToClient(PlaceHolder.ParentContainer.PointToScreen(SelectedContainer.Location)); SelectedContainer.ClientSize = PlaceHolder.ClientSize; SelectedContainer.Size = SelectedContainer.ClientSize; } else { SelectedContainer.Location = PointToClient(SelectedContainer.ParentContainer.PointToScreen(SelectedContainer.Location)); } SelectedContainer.MoveTo(this); } }
public void Dispose() { SelectedContainer?.Dispose(); IsValidItemSelected?.Dispose(); _ContainerItemChangedEventDisposer?.Dispose(); }
private bool HandleScrollByPage(bool up) { // NOTE: This implementation assumes that items are laid out // vertically and the Headers of the TreeViewItems appear above // their ItemsPresenter. The same assumptions are made in WPF. ScrollViewer scrollHost = ItemsControlHelper.ScrollHost; if (scrollHost != null) { double viewportHeight = scrollHost.ViewportHeight; double top; double bottom; (SelectedContainer.HeaderElement ?? SelectedContainer).GetTopAndBottom(scrollHost, out top, out bottom); TreeViewItem selected = null; TreeViewItem next = SelectedContainer; ItemsControl parent = SelectedContainer.ParentItemsControl; if (parent != null) { // We need to start at the root TreeViewItem if we're // scrolling up, but can start at the SelectedItem if // scrolling down. if (up) { while (parent != this) { TreeViewItem parentItem = parent as TreeViewItem; if (parentItem == null) { break; } ItemsControl grandparent = parentItem.ParentItemsControl; if (grandparent == null) { break; } next = parentItem; parent = grandparent; } } int index = parent.ItemContainerGenerator.IndexFromContainer(next); int count = parent.Items.Count; while (parent != null && next != null) { if (next.IsEnabled) { double delta; if (next.HandleScrollByPage(up, scrollHost, viewportHeight, top, bottom, out delta)) { // This item or one of its children was focused return(true); } else if (NumericExtensions.IsGreaterThan(delta, viewportHeight)) { // If the item doesn't fit on the page but it's // already selected, we'll select the next item // even though it doesn't completely fit into // the current view if (selected == SelectedContainer || selected == null) { return(up ? SelectedContainer.HandleUpKey() : SelectedContainer.HandleDownKey()); } break; } else { selected = next; } } index += up ? -1 : 1; if (0 <= index && index < count) { next = parent.ItemContainerGenerator.ContainerFromIndex(index) as TreeViewItem; } else if (parent == this) { // We just finished with the last item in the // TreeView next = null; } else { // Move up the parent chain to the next item while (parent != null) { TreeViewItem oldParent = parent as TreeViewItem; parent = oldParent.ParentItemsControl; if (parent != null) { count = parent.Items.Count; index = parent.ItemContainerGenerator.IndexFromContainer(oldParent) + (up ? -1 : 1); if (0 <= index && index < count) { next = parent.ItemContainerGenerator.ContainerFromIndex(index) as TreeViewItem; break; } else if (parent == this) { next = null; parent = null; } } } } } } if (selected != null) { if (up) { if (selected != SelectedContainer) { return(selected.Focus()); } } else { selected.FocusInto(); } } } return(false); }
/// <summary> /// Change whether a TreeViewItem is selected. /// </summary> /// <param name="itemOrContainer"> /// Item whose selection is changing. /// </param> /// <param name="container"> /// Container of the item whose selection is changing. /// </param> /// <param name="selected"> /// A value indicating whether the TreeViewItem is selected. /// </param> internal void ChangeSelection(object itemOrContainer, TreeViewItem container, bool selected) { // Ignore any change notifications if we're alread in the middle of // changing the selection if (IsSelectionChangeActive) { return; } object oldValue = null; object newValue = null; bool raiseSelectionChanged = false; TreeViewItem element = SelectedContainer; // Start changing the selection IsSelectionChangeActive = true; try { if (selected && container != SelectedContainer) { // Unselect the old value oldValue = SelectedItem; if (SelectedContainer != null) { SelectedContainer.IsSelected = false; SelectedContainer.UpdateContainsSelection(false); } // Select the new value newValue = itemOrContainer; SelectedContainer = container; SelectedContainer.UpdateContainsSelection(true); SelectedItem = itemOrContainer; UpdateSelectedValue(itemOrContainer); raiseSelectionChanged = true; // Scroll the selected item into view. We only want to // scroll the header into view, if possible, because an // expanded TreeViewItem contains all of its child items // as well. ItemsControlHelper.ScrollIntoView(container.HeaderElement ?? container); } else if (!selected && container == SelectedContainer) { // Unselect the old value SelectedContainer.UpdateContainsSelection(false); SelectedContainer = null; SelectedItem = null; SelectedValue = null; oldValue = itemOrContainer; raiseSelectionChanged = true; } container.IsSelected = selected; } finally { // Finish changing the selection IsSelectionChangeActive = false; } // Notify when the selection changes if (raiseSelectionChanged) { if (SelectedContainer != null && AutomationPeer.ListenerExists(AutomationEvents.SelectionItemPatternOnElementSelected)) { AutomationPeer peer = FrameworkElementAutomationPeer.CreatePeerForElement(SelectedContainer); if (peer != null) { peer.RaiseAutomationEvent(AutomationEvents.SelectionItemPatternOnElementSelected); } } if (element != null && AutomationPeer.ListenerExists(AutomationEvents.SelectionItemPatternOnElementRemovedFromSelection)) { AutomationPeer peer = FrameworkElementAutomationPeer.CreatePeerForElement(element); if (peer != null) { peer.RaiseAutomationEvent(AutomationEvents.SelectionItemPatternOnElementRemovedFromSelection); } } OnSelectedItemChanged(new RoutedPropertyChangedEventArgs <object>(oldValue, newValue)); } }