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); }
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; } } } } }