private void RotateThumb_DragStarted(object sender, DragStartedEventArgs e) { this.designerItem = DataContext as ContentControl; if (this.designerItem != null) { //this.canvas = VisualTreeHelper.GetParent(this.designerItem) as Canvas; this.canvas = WpfTreeHelper.FindUpVisualTree <Canvas>(this.designerItem); if (this.canvas != null) { this.centerPoint = this.designerItem.TranslatePoint( new Point(this.designerItem.Width * this.designerItem.RenderTransformOrigin.X, this.designerItem.Height * this.designerItem.RenderTransformOrigin.Y), this.canvas); Point startPoint = Mouse.GetPosition(this.canvas); this.startVector = Point.Subtract(startPoint, this.centerPoint); this.rotateTransform = this.designerItem.RenderTransform as RotateTransform; if (this.rotateTransform == null) { this.designerItem.RenderTransform = new RotateTransform(0); this.initialAngle = 0; } else { this.initialAngle = this.rotateTransform.Angle; } } } }
private List <ScrollViewer> GetMixedGroupScrollViewers() { List <ScrollViewer> scrollViewers = new List <ScrollViewer>(); try { var items = PatternTwoList.Items; foreach (var item in items) { var grid = PatternTwoList.ItemContainerGenerator.ContainerFromItem(item); if (grid != null) { ScrollViewer scroll = WpfTreeHelper.FindChild <ScrollViewer>(grid); if (scroll == null) { continue; } scrollViewers.Add(scroll); } } } catch (Exception) { return(null); } return(scrollViewers); }
private void ResizeThumb_DragStarted(object sender, DragStartedEventArgs e) { this.designerItem = this.DataContext as ContentControl; if (this.designerItem != null) { //this.canvas = VisualTreeHelper.GetParent(this.designerItem) as Canvas; this.canvas = WpfTreeHelper.FindUpVisualTree <Canvas>(this.designerItem); if (this.canvas != null) { this.transformOrigin = this.designerItem.RenderTransformOrigin; //TODO: We need to get ALL render transforms applied by all parents this.rotateTransform = this.designerItem.RenderTransform as RotateTransform; //rotateTransform = Utils.WpfTreeHelper.GetRotationTree(designerItem) as RotateTransform; if (this.rotateTransform != null) { this.angle = this.rotateTransform.Angle * Math.PI / 180.0; } else { this.angle = 0.0d; } AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(this.canvas); if (adornerLayer != null) { this.adorner = new SizeAdorner(this.designerItem); adornerLayer.Add(this.adorner); } } } }
protected override void Invoke(object parameter) { if (TargetControl == null || ScrollViewer == null) { throw new ArgumentNullException($"{ScrollViewer} or {TargetControl} cannot be null"); } // 检查指定的控件是否在指定的 ScrollViewer 中 // TODO: 这里只是指定离它最近的 ScrollViewer,并没有继续向上找 var container = WpfTreeHelper.FindParentControl <ScrollViewer>(TargetControl); if (container == null || container != ScrollViewer) { throw new Exception("The TargetControl is not in the target ScrollViewer"); } // 获取要定位之前 ScrollViewer 目前的滚动位置 var currentScrollPosition = ScrollViewer.VerticalOffset; var point = new Point(0, currentScrollPosition); // 计算出目标位置并滚动 var targetPosition = TargetControl.TransformToVisual(ScrollViewer).Transform(point); ScrollViewer.ScrollToVerticalOffset(targetPosition.Y); }
/// <summary> /// ay 2015-05-23 15:34:08 增加,不需要查找子容器 /// </summary> /// <param name="container"></param> /// <param name="item"></param> /// <returns></returns> public static TreeViewItem GetTreeViewItem2(ItemsControl container, object item) { if (container != null) { if (container.DataContext == item) { return(container as TreeViewItem); } // Expand the current container if (container is TreeViewItem && !((TreeViewItem)container).IsExpanded) { container.SetValue(TreeViewItem.IsExpandedProperty, true); } // Try to generate the ItemsPresenter and the ItemsPanel. // by calling ApplyTemplate. Note that in the // virtualizing case even if the item is marked // expanded we still need to do this step in order to // regenerate the visuals because they may have been virtualized away. container.ApplyTemplate(); ItemsPresenter itemsPresenter = (ItemsPresenter)container.Template.FindName("ItemsHost", container); if (itemsPresenter != null) { itemsPresenter.ApplyTemplate(); } else { // The Tree template has not named the ItemsPresenter, // so walk the descendents and find the child. itemsPresenter = WpfTreeHelper.FindVisualChild <ItemsPresenter>(container); if (itemsPresenter == null) { container.UpdateLayout(); itemsPresenter = WpfTreeHelper.FindVisualChild <ItemsPresenter>(container); } } Panel itemsHostPanel = (Panel)VisualTreeHelper.GetChild(itemsPresenter, 0); // Ensure that the generator for this panel has been created. UIElementCollection children = itemsHostPanel.Children; MyVirtualizingStackPanel virtualizingPanel = itemsHostPanel as MyVirtualizingStackPanel; for (int i = 0, count = container.Items.Count; i < count; i++) { TreeViewItem subContainer = (TreeViewItem)container.ItemContainerGenerator.ContainerFromIndex(i); if (subContainer.DataContext == item) { subContainer.IsExpanded = false; return(subContainer as TreeViewItem); } } } return(null); }
public override void OnApplyTemplate() { base.OnApplyTemplate(); bd = GetTemplateChild("SelectTen") as Border; ContentPresenter cp = GetTemplateChild("contentPresenter") as ContentPresenter; _EditContent = GetTemplateChild("contentPresenter1") as ContentPresenter; if (_column.IsNotNull() && _column.Formatter.IsNotNull()) { var _b = BindingOperations.GetBinding(cp, ContentPresenter.ContentProperty); Binding newbind = new Binding { Converter = _column.Formatter, Path = _b.Path, Mode = _b.Mode, RelativeSource = _b.RelativeSource }; BindingOperations.ClearBinding(cp, ContentPresenter.ContentProperty); BindingOperations.SetBinding(cp, ContentPresenter.ContentProperty, newbind); } cp.ApplyTemplate(); tb = WpfTreeHelper.FindChild <AyText>(cp); if (tb.IsNotNull()) { tb.TextWrapping = CellTextWrapping; if (CellCharacterEllipsis == TextTrimming.CharacterEllipsis) { ToolTip tt = new System.Windows.Controls.ToolTip(); AyText ttb = new AyText(); ttb.SetBinding(AyText.TextProperty, new Binding { Source = cp.Content }); tt.Content = ttb; tb.ToolTip = tt; if (dpd != null) { dpd.AddValueChanged(tb, ActualWidthPropertyChangedHandler); } if (tb.ActualWidth < tb.DesiredSize.Width) { tt.Visibility = Visibility.Visible; } else { tt.Visibility = Visibility.Collapsed; } } tb.TextTrimming = CellCharacterEllipsis; } }
/// <summary> /// Initializes a new instance of the AutoScroller class. /// </summary> /// <param name="itemsControl">The ItemsControl that is scrolled.</param> /// <exception cref="ArgumentNullException">itemsControl is null.</exception> public AutoScroller(ItemsControl itemsControl) { if (itemsControl == null) { throw new ArgumentNullException("itemsControl"); } this.itemsControl = itemsControl; this.scrollViewer = WpfTreeHelper.FindChild <ScrollViewer>(itemsControl); this.scrollViewer.ScrollChanged += this.OnScrollChanged; this.scrollContent = WpfTreeHelper.FindChild <ScrollContentPresenter>(this.scrollViewer); this.autoScroll.Tick += delegate { this.PreformScroll(); }; this.autoScroll.Interval = TimeSpan.FromMilliseconds(GetRepeatRate()); }
private bool Register() { this.listBox.UpdateLayout(); this.scrollContent = WpfTreeHelper.FindChild <ScrollContentPresenter>(this.listBox); if (this.scrollContent != null) { this.autoScroller = new AutoScroller(this.listBox); this.autoScroller.OffsetChanged += this.OnOffsetChanged; var d = GetSelectionBrush(this.listBox); this.selectionRect = new AyTableViewSelectionAdorner(this.scrollContent, d); this.scrollContent.AdornerLayer.Add(this.selectionRect); this.selector = new AyTableViewItemsControlSelector(this.listBox); this.listBox.PreviewMouseLeftButtonDown += this.OnPreviewMouseLeftButtonDown; this.listBox.MouseLeftButtonUp += this.OnMouseLeftButtonUp; this.listBox.MouseMove += this.OnMouseMove; } return(this.scrollContent != null); }
private void AyTreeViewItemCollapsed(object sender, RoutedEventArgs e) { TreeViewItem tvi = e.OriginalSource as TreeViewItem; //tvi.IsExpanded = false; if (tvi != null && tvi.Items.Count > 0) { itemhost = WpfTreeHelper.GetChildObject <ItemsPresenter>(tvi, "ItemsHost"); // double targetHeight = 0; ////添加动画 foreach (var item in tvi.Items) { AyTreeViewItemModel temp = item as AyTreeViewItemModel; if (temp.RelativeItem != null) { temp.ParentCategory.IsExpanded = false; break; } } if (itemhost != null) { itemhost.Visibility = Visibility.Visible; DoubleAnimationUsingKeyFrames HeightKey = new DoubleAnimationUsingKeyFrames(); HeightKey.Duration = new Duration(TimeSpan.FromMilliseconds(ExpandedTime)); HeightKey.FillBehavior = FillBehavior.Stop; EasingDoubleKeyFrame HeightKey0 = new EasingDoubleKeyFrame(itemhost.ActualHeight, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0))); EasingDoubleKeyFrame HeightKey1 = new EasingDoubleKeyFrame(0); HeightKey1.EasingFunction = easyOut; HeightKey.KeyFrames.Add(HeightKey0); HeightKey.KeyFrames.Add(HeightKey1); HeightKey.Completed += HeightKey_Completed; itemhost.BeginAnimation(ItemsPresenter.HeightProperty, HeightKey); } } e.Handled = true; }
private static void ExecuteSetters(AyVisualState item, FrameworkElement w) { Window _parentWindow = null; if (w is Window) { _parentWindow = w as Window; } else { _parentWindow = Window.GetWindow(w); } UserControl _parentUserControl = null; if (w is UserControl) { _parentUserControl = w as UserControl; } else { _parentUserControl = WpfTreeHelper.FindParentControl <UserControl>(w); } Page _parentPage = null; if (w is Page) { _parentPage = w as Page; } else { _parentPage = WpfTreeHelper.FindParentControl <Page>(w); } foreach (var setter2 in item.Setters) { if (setter2 is DataSetter) { var setter = setter2 as DataSetter; if (setter.TargetName != null) { switch (setter.Scope) { case DataSetterScope.Current: var _CurrentFind = w.FindName(setter.TargetName) as UIElement; if (_CurrentFind != null) { _CurrentFind.SetValue(setter.Property, setter.Value); } break; case DataSetterScope.ParentWindow: if (_parentWindow != null) { var _ParentWindowFind = _parentWindow.FindName(setter.TargetName) as UIElement; if (_ParentWindowFind != null) { _ParentWindowFind.SetValue(setter.Property, setter.Value); } } break; case DataSetterScope.ParentPage: if (_parentPage != null) { var _ParentPageFind = _parentPage.FindName(setter.TargetName) as UIElement; if (_ParentPageFind != null) { _ParentPageFind.SetValue(setter.Property, setter.Value); } } break; } } } else if (setter2 is ResourceSetter) { var setter = setter2 as ResourceSetter; if (setter.TargetName != null) { switch (setter.Scope) { case ResourceSetterScope.FindInPageResource: if (_parentPage != null) { var _resources = _parentPage.Resources; _resources[setter.TargetName] = Convert.ChangeType(setter.ResourceValue, setter.ResourceType); } break; case ResourceSetterScope.FindInApplicationResource: if (_parentPage != null) { var _resources = Application.Current.Resources; _resources[setter.TargetName] = Convert.ChangeType(setter.ResourceValue, setter.ResourceType); } break; case ResourceSetterScope.FindInWindowResource: if (_parentPage != null) { var _resources = _parentWindow.Resources; _resources[setter.TargetName] = Convert.ChangeType(setter.ResourceValue, setter.ResourceType); } break; case ResourceSetterScope.FindInUserControlResource: if (_parentPage != null) { var _resources = _parentUserControl.Resources; _resources[setter.TargetName] = Convert.ChangeType(setter.ResourceValue, setter.ResourceType); } break; } } } } }
/// <summary> /// 每个时常都是一样的,暂定200 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void AyTreeViewItemExpanded(object sender, RoutedEventArgs e) { TreeViewItem tvi = e.OriginalSource as TreeViewItem; if (tvi != null && tvi.Items.Count > 0) { int executeTime = 0;//执行time误差 int durationTime = 300; double targetHeight = 0; int expandNode = 0; //添加动画 foreach (var item in tvi.Items) { AyTreeViewItemModel temp = item as AyTreeViewItemModel; temp.ParentCategory.IsExpanded = true; if (temp.RelativeItem == null) { temp.RelativeItem = TreeViewHelper.GetTreeViewItem2(tvi, temp); } expandNode += GetTreeViewItemCount(temp); if (IsNeedMoveAndOpacity) { if (temp.RelativeItem != null) { temp.RelativeItem.Opacity = 0; } } } targetHeight += (TreeViewItemHeight + OtherBorderTreeViewHeight) * expandNode;//计算当前展开的有多少个item //set itemhost的高度 var itemhost = WpfTreeHelper.GetChildObject <ItemsPresenter>(tvi, "ItemsHost"); if (itemhost != null) { itemhost.Visibility = Visibility.Visible; DoubleAnimationUsingKeyFrames HeightKey = new DoubleAnimationUsingKeyFrames(); HeightKey.FillBehavior = FillBehavior.Stop; EasingDoubleKeyFrame HeightKey0 = new EasingDoubleKeyFrame(0, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0))); HeightKey.KeyFrames.Add(HeightKey0); if (IsNeedMoveAndOpacity) { EasingDoubleKeyFrame HeightKey1 = new EasingDoubleKeyFrame(targetHeight, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(ExpandedTime)), easyOut); HeightKey.KeyFrames.Add(HeightKey1); } else { EasingDoubleKeyFrame HeightKey1 = new EasingDoubleKeyFrame(targetHeight, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(ExpandedTime))); HeightKey.KeyFrames.Add(HeightKey1); } itemhost.BeginAnimation(ItemsPresenter.HeightProperty, null); itemhost.BeginAnimation(ItemsPresenter.HeightProperty, HeightKey); } if (IsNeedMoveAndOpacity) { int index = 0; int endIndex = tvi.Items.Count; //添加动画 foreach (var item in tvi.Items) { AyTreeViewItemModel temp = item as AyTreeViewItemModel; if (temp.RelativeItem != null) { index++; DoubleAnimationUsingKeyFrames OpacityKey = new DoubleAnimationUsingKeyFrames(); EasingDoubleKeyFrame opacityKey10 = new EasingDoubleKeyFrame(0, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0))); EasingDoubleKeyFrame opacityKey0 = new EasingDoubleKeyFrame(0, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(executeTime))); EasingDoubleKeyFrame opacityKey1 = new EasingDoubleKeyFrame(1, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(executeTime + durationTime))); opacityKey1.EasingFunction = easyIn; OpacityKey.KeyFrames.Add(opacityKey0); OpacityKey.KeyFrames.Add(opacityKey1); ThicknessAnimationUsingKeyFrames MarginKey = new ThicknessAnimationUsingKeyFrames(); var tempThickness = GetTargetThickness(temp.Depth); MarginKey.KeyFrames.Add(new EasingThicknessKeyFrame(new Thickness(0, 0, 0, 0), KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0)))); MarginKey.KeyFrames.Add(new EasingThicknessKeyFrame(new Thickness(0, 0, 0, 0), KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(executeTime)))); MarginKey.KeyFrames.Add(new EasingThicknessKeyFrame(tempThickness, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(executeTime + durationTime)))); opacityKey1.EasingFunction = easyIn; temp.RelativeItem.BeginAnimation(TreeViewItem.OpacityProperty, null); temp.RelativeItem.BeginAnimation(TreeViewItem.OpacityProperty, OpacityKey); temp.RelativeItem.BeginAnimation(TreeViewItem.PaddingProperty, null); temp.RelativeItem.BeginAnimation(TreeViewItem.PaddingProperty, MarginKey); OpacityKey = null; MarginKey = null; executeTime += 30; if (endIndex == index) { temp.RelativeItem.BringIntoView(); } } } executeTime = 0; } } e.Handled = true; }