public ScrollToHorizontalOffset ( double offset ) : void | ||
offset | double | The position that the content scrolls to. |
Résultat | void |
/// <summary> /// Scroll a ScrollViewer horizontally by a given offset. /// </summary> /// <param name="viewer">The ScrollViewer.</param> /// <param name="offset">The horizontal offset to scroll.</param> private static void ScrollByHorizontalOffset(ScrollViewer viewer, double offset) { Debug.Assert(viewer != null, "viewer should not be null!"); offset += viewer.HorizontalOffset; offset = Math.Max(Math.Min(offset, viewer.ExtentWidth), 0); viewer.ScrollToHorizontalOffset(offset); }
private void scrollViewer_Loaded(object sender, EventArgs e) { scrollViewer = (ScrollViewer)sender; if (scrollToIndex != -1) { scrollViewer.ScrollToHorizontalOffset(scrollToIndex); } }
private static void OnScrollViewerChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) { ScrollViewerOffsetMediator scrollViewerOffsetMediator = (ScrollViewerOffsetMediator)o; System.Windows.Controls.ScrollViewer newValue = (System.Windows.Controls.ScrollViewer)e.NewValue; if (newValue != null) { newValue.ScrollToHorizontalOffset(scrollViewerOffsetMediator.HorizontalOffset); } }
//Todo: This was done by experimentation and is by all accounts an ugly hack! Research into a better solution should be undertaken! private void ScrollToCurrentSlide(ScrollViewer scrollviewer) { var slideshowDialog = this.DataContext as EditSlideshowDialog; if(slideshowDialog != null && slideshowDialog.Slideshow != null) { var slideShow = slideshowDialog.Slideshow; var currentSlide = slideShow.CurrentSlide; var index = slideShow.Slides.IndexOf(currentSlide); if (index <= 1) { scrollviewer.ScrollToHorizontalOffset(1); } else { scrollviewer.ScrollToHorizontalOffset( 138 * (index-1)); } } }
static void AutoScroll(Point positionInScrollViewer, ScrollViewer scrollViewer, Point? positionInLogicalView, FrameworkElement logicalView, double scrollOnDragThresholdX, double scrollOnDragThresholdY, int scrollOnDragOffset) { double scrollViewerWidth = scrollViewer.ActualWidth; double scrollViewerHeight = scrollViewer.ActualHeight; double logicalViewWidth = 0; double logicalViewHeight = 0; if (logicalView != null) { logicalViewWidth = logicalView.ActualWidth; logicalViewHeight = logicalView.ActualHeight; } int heightToScroll = 0; int widthToScroll = 0; if (positionInScrollViewer.X > (scrollViewerWidth - scrollOnDragThresholdX) && (positionInLogicalView == null || positionInLogicalView.Value.X < (logicalViewWidth - scrollBuffer))) { widthToScroll = scrollOnDragOffset; } else if (positionInScrollViewer.X < scrollOnDragThresholdX && (positionInLogicalView == null || positionInLogicalView.Value.X > scrollBuffer)) { widthToScroll = -scrollOnDragOffset; } if (positionInScrollViewer.Y > (scrollViewerHeight - scrollOnDragThresholdY) && (positionInLogicalView == null || positionInLogicalView.Value.Y < logicalViewHeight - scrollBuffer)) { heightToScroll = scrollOnDragOffset; } else if (positionInScrollViewer.Y < scrollOnDragThresholdY && (positionInLogicalView == null || positionInLogicalView.Value.Y > scrollBuffer)) { heightToScroll = -scrollOnDragOffset; } if (widthToScroll != 0 || heightToScroll != 0) { scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + heightToScroll); scrollViewer.ScrollToHorizontalOffset(scrollViewer.HorizontalOffset + widthToScroll); } }
public void setChildHidden(Point point, ref ScrollViewer scrollviewer) { DrawingVisual visual = drawingCanvas.GetVisual(point); if (visual != null && visualDict.ContainsKey(visual)) { if ( !(pieces[visualDict[visual]].isChildTreeHidden==false&& pieces[visualDict[visual]].sonNum == 0) ) { pieces[visualDict[visual]].isChildTreeHidden = !pieces[visualDict[visual]].isChildTreeHidden; int it = visualDict[visual]; visualDict.Clear(); drawingCanvas.clear(); double preX = pieces[it].topLeftCorner.X; calculate(); double nowX = pieces[it].topLeftCorner.X; if (scrollviewer.HorizontalOffset - preX + nowX >= 0) scrollviewer.ScrollToHorizontalOffset(scrollviewer.HorizontalOffset - preX + nowX); else shiftChildTree(-1, pieces.Count, preX - nowX); drawTreeGraph(); double maxMostRight = 0; for (int i = 0; i < mostRight.Count; i++) { if (maxMostRight < mostRight[i]) maxMostRight = mostRight[i]; } if (drawingCanvas.Width < maxMostRight + Constant.hDelta) drawingCanvas.Width = maxMostRight + Constant.hDelta; if (drawingCanvas.Height < (maxDepth) * Constant.vDelta) drawingCanvas.Height = (maxDepth) * Constant.vDelta; } } }
private void set(ScrollChangedEventArgs e, ScrollViewer sv, Canvas canvas) { if (e.ExtentHeightChange != 0 || e.ExtentWidthChange != 0) { Point? targetBefore = null; Point? targetNow = null; if (!lastMousePositionOnTarget.HasValue) { if (lastCenterPositionOnTarget.HasValue) { var centerOfViewport = new Point(sv.ViewportWidth / 2, sv.ViewportHeight / 2); Point centerOfTargetNow = sv.TranslatePoint(centerOfViewport, canvas); targetBefore = lastCenterPositionOnTarget; targetNow = centerOfTargetNow; } } else { targetBefore = lastMousePositionOnTarget; targetNow = Mouse.GetPosition(canvas); lastMousePositionOnTarget = null; } if (targetBefore.HasValue) { double dXInTargetPixels = targetNow.Value.X - targetBefore.Value.X; double dYInTargetPixels = targetNow.Value.Y - targetBefore.Value.Y; double multiplicatorX = e.ExtentWidth / canvas.Width; double multiplicatorY = e.ExtentHeight / canvas.Height; double newOffsetX = sv.HorizontalOffset - dXInTargetPixels * multiplicatorX; double newOffsetY = sv.VerticalOffset - dYInTargetPixels * multiplicatorY; if (double.IsNaN(newOffsetX) || double.IsNaN(newOffsetY)) { return; } sv.ScrollToHorizontalOffset(newOffsetX); sv.ScrollToVerticalOffset(newOffsetY); } } }
/// <summary> /// Scroll the desired element into the ScrollViewer's viewport. /// </summary> /// <param name="viewer">The ScrollViewer.</param> /// <param name="element">The element to scroll into view.</param> /// <param name="horizontalMargin">The margin to add on the left or right. /// </param> /// <param name="verticalMargin">The margin to add on the top or bottom. /// </param> /// <param name="duration">The duration of the animation.</param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="viewer" /> is null. /// </exception> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="element" /> is null. /// </exception> public static void ScrollIntoView(this ScrollViewer viewer, FrameworkElement element, double horizontalMargin, double verticalMargin, Duration duration) { if (viewer == null) { throw new ArgumentNullException("viewer"); } else if (element == null) { throw new ArgumentNullException("element"); } // Get the position of the element relative to the ScrollHost Rect?itemRect = element.GetBoundsRelativeTo(viewer); if (itemRect == null) { return; } // Scroll vertically double verticalOffset = viewer.VerticalOffset; double verticalDelta = 0; double hostBottom = viewer.ViewportHeight; double itemBottom = itemRect.Value.Bottom + verticalMargin; if (hostBottom < itemBottom) { verticalDelta = itemBottom - hostBottom; verticalOffset += verticalDelta; } double itemTop = itemRect.Value.Top - verticalMargin; if (itemTop - verticalDelta < 0) { verticalOffset -= verticalDelta - itemTop; } // Scroll horizontally double horizontalOffset = viewer.HorizontalOffset; double horizontalDelta = 0; double hostRight = viewer.ViewportWidth; double itemRight = itemRect.Value.Right + horizontalMargin; if (hostRight < itemRight) { horizontalDelta = itemRight - hostRight; horizontalOffset += horizontalDelta; } double itemLeft = itemRect.Value.Left - horizontalMargin; if (itemLeft - horizontalDelta < 0) { horizontalOffset -= horizontalDelta - itemLeft; } if (duration == TimeSpan.Zero) { viewer.ScrollToVerticalOffset(verticalOffset); viewer.ScrollToHorizontalOffset(horizontalOffset); } else { Storyboard storyboard = new Storyboard(); SetVerticalOffset(viewer, viewer.VerticalOffset); SetHorizontalOffset(viewer, viewer.HorizontalOffset); DoubleAnimation verticalOffsetAnimation = new DoubleAnimation { To = verticalOffset, Duration = duration }; DoubleAnimation horizontalOffsetAnimation = new DoubleAnimation { To = verticalOffset, Duration = duration }; Storyboard.SetTarget(verticalOffsetAnimation, viewer); Storyboard.SetTarget(horizontalOffsetAnimation, viewer); Storyboard.SetTargetProperty(horizontalOffsetAnimation, new PropertyPath(ScrollViewerExtensions.HorizontalOffsetProperty)); Storyboard.SetTargetProperty(verticalOffsetAnimation, new PropertyPath(ScrollViewerExtensions.VerticalOffsetProperty)); storyboard.Children.Add(verticalOffsetAnimation); storyboard.Children.Add(horizontalOffsetAnimation); storyboard.Begin(); } }
internal void ScrollIntoView(FrameworkElement element) { // Get the ScrollHost ScrollViewer scrollHost = ScrollHost; if (scrollHost == null) { return; } // Get the position of the element relative to the ScrollHost GeneralTransform transform = null; try { transform = element.TransformToVisual(scrollHost); } catch (ArgumentException) { // Ignore failures when not in the visual tree return; } Rect itemRect = new Rect( transform.Transform(new Point()), transform.Transform(new Point(element.ActualWidth, element.ActualHeight))); // Scroll vertically double verticalOffset = scrollHost.VerticalOffset; double verticalDelta = 0; double hostBottom = scrollHost.ViewportHeight; double itemBottom = itemRect.Bottom; if (hostBottom < itemBottom) { verticalDelta = itemBottom - hostBottom; verticalOffset += verticalDelta; } double itemTop = itemRect.Top; if (itemTop - verticalDelta < 0) { verticalOffset -= verticalDelta - itemTop; } scrollHost.ScrollToVerticalOffset(verticalOffset); // Scroll horizontally double horizontalOffset = scrollHost.HorizontalOffset; double horizontalDelta = 0; double hostRight = scrollHost.ViewportWidth; double itemRight = itemRect.Right; if (hostRight < itemRight) { horizontalDelta = itemRight - hostRight; horizontalOffset += horizontalDelta; } double itemLeft = itemRect.Left; if (itemLeft - horizontalDelta < 0) { horizontalOffset -= horizontalDelta - itemLeft; } scrollHost.ScrollToHorizontalOffset(horizontalOffset); }
static ScrollViewer AddScrollViewerMouseWheelSupport(ScrollViewer scrollViewer, double scrollAmount) { if(mouseWheelHelper != null) { mouseWheelHelper.MouseWheelMoved += (source, eventArgs) => { var delta = eventArgs.WheelDelta; delta *= scrollAmount; if (eventArgs.IsHorizontal) { var newOffset = scrollViewer.HorizontalOffset - delta; if (newOffset > scrollViewer.ScrollableWidth) newOffset = scrollViewer.ScrollableWidth; else if (newOffset < 0) newOffset = 0; scrollViewer.ScrollToHorizontalOffset(newOffset); } else { var newOffset = scrollViewer.VerticalOffset - delta; if (newOffset > scrollViewer.ScrollableHeight) newOffset = scrollViewer.ScrollableHeight; else if (newOffset < 0) newOffset = 0; scrollViewer.ScrollToVerticalOffset(newOffset); } eventArgs.BrowserEventHandled = true; }; } return scrollViewer; }
/// <summary> /// This is the command scroll adjustment code which synchronizes two ScrollViewer instances. /// </summary> /// <param name="sv">ScrollViewer to adjust</param> /// <param name="e">Change in the source</param> /// <param name="hadjust">Horizontal adjustment</param> /// <param name="vadjust">Vertical adjustment</param> private static void AdjustScrollPosition(ScrollViewer sv, ScrollChangedEventArgs e, double hadjust, double vadjust) { if (e.HorizontalChange != 0 || e.ExtentWidthChange != 0) { if (e.HorizontalOffset == 0) sv.ScrollToLeftEnd(); else if (e.HorizontalOffset >= e.ExtentWidth-5) sv.ScrollToRightEnd(); else if (e.ExtentWidth + hadjust == sv.ExtentWidth) sv.ScrollToHorizontalOffset(e.HorizontalOffset + hadjust); else sv.ScrollToHorizontalOffset((sv.ExtentWidth * (e.HorizontalOffset / e.ExtentWidth)) + hadjust); } if (e.VerticalChange != 0 || e.ExtentHeightChange != 0) { if (e.VerticalOffset == 0) sv.ScrollToTop(); else if (e.VerticalOffset >= e.ExtentHeight-5) sv.ScrollToBottom(); else if (e.ExtentHeight + vadjust == sv.ExtentHeight) sv.ScrollToVerticalOffset(e.VerticalOffset + vadjust); else sv.ScrollToVerticalOffset((sv.ExtentHeight * (e.VerticalOffset / e.ExtentHeight)) + vadjust); } }
protected override void OnPreviewMouseMove(MouseEventArgs e) { if (e.LeftButton == Input.MouseButtonState.Pressed) { try { var p = e.GetPosition(this); if (p.X < 0 || p.X > this.ActualWidth || p.Y < 0 || p.Y > this.ActualHeight) { OnPreviewMouseLeftButtonUp(new MouseButtonEventArgs(e.MouseDevice, e.Timestamp, Input.MouseButton.Left)); return; } if (double.IsNaN(last) || double.IsNaN(start)) { startTime = e.Timestamp; if (orientation == Orientation.Horizontal) { start = p.X; last = p.X; } else { start = p.Y; last = p.Y; } return; } if (orientation == Orientation.Horizontal) { var x = p.X; var length = scrollViewer.HorizontalOffset - x + last; if (length < 0) { length = 0; } else if (length > scrollViewer.ScrollableWidth) { length = scrollViewer.ScrollableWidth; } scrollViewer.ScrollToHorizontalOffset(length); last = x; } else { var y = p.Y; var length = scrollViewer.VerticalOffset - y + last; if (length < 0) { length = 0; } else if (length > scrollViewer.ScrollableHeight) { length = scrollViewer.ScrollableHeight; } scrollViewer.ScrollToVerticalOffset(length); last = y; } e.Handled = true; } catch { } } }
/// <summary> /// Attempts to extract the scrollbars that are within the scrollviewers /// visual tree. When extracted, event handlers are added to their ValueChanged events. /// </summary> private static void GetScrollBarsForScrollViewer(ScrollViewer scrollViewer) { ScrollBar scroll = GetScrollBar(scrollViewer, Orientation.Vertical); if (scroll != null) { // save a reference to this scrollbar on the attached property scrollViewer.SetValue(VerticalScrollBarProperty, scroll); // scroll the scrollviewer scrollViewer.ScrollToVerticalOffset(GetVerticalOffset(scrollViewer)); // handle the changed event to update the exposed VerticalOffset scroll.ValueChanged += (s, e) => SetVerticalOffset(scrollViewer, e.NewValue); } scroll = GetScrollBar(scrollViewer, Orientation.Horizontal); if (scroll != null) { // save a reference to this scrollbar on the attached property scrollViewer.SetValue(HorizontalScrollBarProperty, scroll); // scroll the scrollviewer scrollViewer.ScrollToHorizontalOffset(GetHorizontalOffset(scrollViewer)); // handle the changed event to update the exposed HorizontalOffset scroll.ValueChanged += (s, e) => scrollViewer.SetValue(HorizontalOffsetProperty, e.NewValue); } }
/// <summary> /// 滑动改变 /// </summary> /// <param name="isLeft">滑动方向 </param> /// <param name="scrollViewer">自动调整的控件</param> /// <param name="width">单个间隔的宽度</param> public int TranslationChanged(bool isLeft,ScrollViewer scrollViewer,double width) { int newSelected; if (!isLeft) { newSelected = _selecter + 1; } else { newSelected = _selecter - 1; } //循环切换 if (newSelected < 0) newSelected = _effects.Count - 1; if (newSelected >= _effects.Count) newSelected = 0; //自动调整位置 if (newSelected >= 3) { //判断方向 if (scrollViewer.HorizontalScrollBarVisibility != ScrollBarVisibility.Disabled) { scrollViewer.ScrollToHorizontalOffset(width * (newSelected - 2)); } else { scrollViewer.ScrollToVerticalOffset(width * (newSelected - 2)); } } else { if (scrollViewer.HorizontalScrollBarVisibility != ScrollBarVisibility.Disabled) { scrollViewer.ScrollToHorizontalOffset(0); } else { scrollViewer.ScrollToVerticalOffset(0); } } //变化效果 SelectedChanged(newSelected); return newSelected; }
private static void ScrollByHorizontalOffset(ScrollViewer viewer, double offset) { offset += viewer.HorizontalOffset; offset = ScrollViewerExtensions.CoerceHorizontalOffset(viewer, offset); viewer.ScrollToHorizontalOffset(offset); }
public static void ScrollToCenter(ScrollViewer scrollViewer) { scrollViewer.ScrollToHorizontalOffset(scrollViewer.ExtentWidth / 2 - scrollViewer.Width / 2); scrollViewer.ScrollToVerticalOffset(scrollViewer.ExtentHeight / 2 - scrollViewer.Height / 2); }
public void BranchHidden(int position, bool isFunctionHidden, ref ScrollViewer scrollviewer) { if (isFunctionHidden) { setBranchHidden(pieces[position].functionName); } else { setBranchHidden(position); } visualDict.Clear(); drawingCanvas.clear(); double preX = pieces[position].topLeftCorner.X; calculate(); double nowX = pieces[position].topLeftCorner.X; if (scrollviewer.HorizontalOffset - preX + nowX >= 0) scrollviewer.ScrollToHorizontalOffset(scrollviewer.HorizontalOffset - preX + nowX); else shiftChildTree(-1, pieces.Count, preX - nowX); drawTreeGraph(); double maxMostRight = 0; for (int i = 0; i < mostRight.Count; i++) { if (maxMostRight < mostRight[i]) maxMostRight = mostRight[i]; } if (drawingCanvas.Width < maxMostRight + Constant.hDelta) drawingCanvas.Width = maxMostRight + Constant.hDelta; if (drawingCanvas.Height < (maxDepth) * Constant.vDelta) drawingCanvas.Height = (maxDepth) * Constant.vDelta; }
/// <summary> /// Scroll a ScrollViewer horizontally by a given offset. /// </summary> /// <param name="viewer">The ScrollViewer.</param> /// <param name="offset">The horizontal offset to scroll.</param> private static void ScrollByHorizontalOffset(ScrollViewer viewer, double offset) { Debug.Assert(viewer != null, "viewer should not be null!"); offset += viewer.HorizontalOffset; offset = CoerceHorizontalOffset(viewer, offset); viewer.ScrollToHorizontalOffset(offset); }
private static void addToHorizontalScrollGroup(string horizontalGroupName, ScrollViewer scrollViewer) { if (horizontalScrollGroups.ContainsKey(horizontalGroupName)) { scrollViewer.ScrollToHorizontalOffset(horizontalScrollGroups[horizontalGroupName].Offset); horizontalScrollGroups[horizontalGroupName].ScrollViewers.Add(scrollViewer); } else { horizontalScrollGroups.Add(horizontalGroupName, new OffSetContainer { ScrollViewers = new List<ScrollViewer> { scrollViewer }, Offset = scrollViewer.HorizontalOffset }); } scrollViewer.ScrollChanged += ScrollViewer_HorizontalScrollChanged; }
/// <summary> /// Добавляет изображение в StackPanel с одной стороны и удоляет изображение с другой ///</summary> ///<remarks>Следит что бы было только 3 изображения в StackPanel</remarks> /// <param name="panel">ScrollViewer</param> public void AddAndRemoveImage(ScrollViewer panel) { var someWidth = panel.HorizontalOffset; //текущее состояние прокрутки Image img; if (Math.Abs(someWidth - panel.ScrollableWidth) < 0.001) //ScrollableWidth - максимально возможная прокрутка { img = new Image { Width = _size.Width, Height = _size.Height, Source = NextImg(NextImage.Right) }; StackPanel1.Children.Add(img); if (StackPanel1.Children.Count > 3) StackPanel1.Children.RemoveAt(0); panel.ScrollToHorizontalOffset(panel.ScrollableWidth - img.Width); } if(StackPanel1.Children.Count > 1) _indexCurr = ImageList.Children.IndexOf(StackPanel1.Children[1]); if (Math.Abs(someWidth) > 0.001) return; img = new Image { Width = _size.Width, Height = _size.Height, Source = NextImg(NextImage.Left) }; StackPanel1.Children.Insert(0, img); if (StackPanel1.Children.Count > 3) StackPanel1.Children.RemoveAt(StackPanel1.Children.Count - 1); panel.ScrollToHorizontalOffset(0 + img.Width); if (StackPanel1.Children.Count > 1) _indexCurr = ImageList.Children.IndexOf(StackPanel1.Children[1]); }