/// <summary> /// Gets the value of the ConnectingLineInfo attached property for a /// specified TreeViewItem. /// </summary> /// <param name="element"> /// The TreeViewItem from which the property value is read. /// </param> /// <returns> /// The ConnectingLineInfo property value for the TreeViewItem. /// </returns> internal static TreeViewItemConnectingLineInfo GetConnectingLineInfo(TreeViewItem element) { if (element == null) { throw new ArgumentNullException("element"); } // Get the info and create on demand if necessary TreeViewItemConnectingLineInfo info = element.GetValue(ConnectingLineInfoProperty) as TreeViewItemConnectingLineInfo; if (info == null) { info = new TreeViewItemConnectingLineInfo(element); element.SetValue(ConnectingLineInfoProperty, info); } return info; }
/// <summary> /// Gets the value of the ConnectingLineInfo attached property for a /// specified TreeViewItem. /// </summary> /// <param name="element"> /// The TreeViewItem from which the property value is read. /// </param> /// <returns> /// The ConnectingLineInfo property value for the TreeViewItem. /// </returns> internal static TreeViewItemConnectingLineInfo GetConnectingLineInfo(TreeViewItem element) { if (element == null) { throw new ArgumentNullException("element"); } // Get the info and create on demand if necessary TreeViewItemConnectingLineInfo info = element.GetValue(ConnectingLineInfoProperty) as TreeViewItemConnectingLineInfo; if (info == null) { info = new TreeViewItemConnectingLineInfo(element); element.SetValue(ConnectingLineInfoProperty, info); } return(info); }
/// <summary> /// IsHeaderOfProperty property changed handler. /// </summary> /// <param name="d"> /// FrameworkElement that changed its IsHeaderOf TreeViewItem. /// </param> /// <param name="e">Event arguments.</param> private static void OnIsHeaderOfPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { FrameworkElement source = d as FrameworkElement; TreeViewItem value = e.NewValue as TreeViewItem; if (value != null) { TreeViewItemConnectingLineInfo info = GetConnectingLineInfo(value); info.Header = source; } else { value = e.OldValue as TreeViewItem; if (value != null) { TreeViewItemConnectingLineInfo info = GetConnectingLineInfo(value); info.Header = null; } } }
/// <summary> /// IsExpanderButtonOfProperty property changed handler. /// </summary> /// <param name="d"> /// ToggleButton that changed its IsExpanderButtonOf TreeViewItem. /// </param> /// <param name="e">Event arguments.</param> private static void OnIsExpanderButtonOfPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ToggleButton source = d as ToggleButton; TreeViewItem value = e.NewValue as TreeViewItem; if (value != null) { TreeViewItemConnectingLineInfo info = GetConnectingLineInfo(value); info.ExpanderButton = source; } else { value = e.OldValue as TreeViewItem; if (value != null) { TreeViewItemConnectingLineInfo info = GetConnectingLineInfo(value); info.ExpanderButton = null; } } }
/// <summary> /// IsHorizontalConnectingLineOfProperty property changed handler. /// </summary> /// <param name="d"> /// Line that changed its IsHorizontalConnectingLineOf TreeViewItem. /// </param> /// <param name="e">Event arguments.</param> private static void OnIsHorizontalConnectingLineOfPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { Line source = d as Line; TreeViewItem value = e.NewValue as TreeViewItem; if (value != null) { TreeViewItemConnectingLineInfo info = GetConnectingLineInfo(value); info.HorizontalConnectingLine = source; } else { value = e.OldValue as TreeViewItem; if (value != null) { TreeViewItemConnectingLineInfo info = GetConnectingLineInfo(value); info.HorizontalConnectingLine = null; } } }
/// <summary> /// Position the vertical connecting line in the TreeViewItem. /// </summary> private void PositionVerticalConnectingLine() { // Nothing to position if we don't have a connecting line if (VerticalConnectingLine == null) { return; } // Nothing to position if there are no nested items or if we're // collapsed if (!Item.IsExpanded || Item.Items.Count <= 0) { VerticalConnectingLine.Visibility = Visibility.Collapsed; return; } // Get the last nested item (which tells us how far down to draw the // connecting line) TreeViewItem lastItem = Item.ItemContainerGenerator.ContainerFromIndex(Item.Items.Count - 1) as TreeViewItem; if (lastItem == null) { VerticalConnectingLine.Visibility = Visibility.Collapsed; return; } // Get the element on the last nested item that the vertical line // should connect to TreeViewItemConnectingLineInfo info = TreeViewConnectingLines.GetConnectingLineInfo(lastItem); FrameworkElement connection = lastItem.HasItems ? (FrameworkElement)info.ExpanderButton : (FrameworkElement)info.HorizontalConnectingLine; if (connection != null) { Rect?bounds = connection.GetBoundsRelativeTo(VerticalConnectingLine); if (bounds == null) { return; } double bottomY = bounds.Value.Y; ////double centerX = (bounds.Value.Left + bounds.Value.Right) / 2.0; // Augment the length with the Y offset of the horizontal // connecting line if (!lastItem.HasItems) { bottomY += info.HorizontalConnectingLine.Y1; ////centerX = bounds.Value.Left + info.HorizontalConnectingLine.X1; } ////VerticalConnectingLine.X1 = centerX; ////VerticalConnectingLine.X2 = centerX; VerticalConnectingLine.Y2 = bottomY; } ////// Connect the top of the line to just below the header ////if (Header == null) ////{ //// VerticalConnectingLine.Y1 = 0; ////} ////else ////{ //// Rect? bounds = Header.GetBoundsRelativeTo(VerticalConnectingLine); //// VerticalConnectingLine.Y1 = (bounds != null) ? //// bounds.Value.Y : //// 0; ////} VerticalConnectingLine.Visibility = Visibility.Visible; }