public ScrollToVerticalOffset ( double offset ) : void | ||
offset | double | The position that the content scrolls to. |
return | void |
/// <summary> /// Scroll a ScrollViewer vertically by a given offset. /// </summary> /// <param name="viewer">The ScrollViewer.</param> /// <param name="offset">The vertical offset to scroll.</param> private static void ScrollByVerticalOffset(ScrollViewer viewer, double offset) { Debug.Assert(viewer != null, "viewer should not be null!"); offset += viewer.VerticalOffset; offset = Math.Max(Math.Min(offset, viewer.ExtentHeight), 0); viewer.ScrollToVerticalOffset(offset); }
/// <summary> /// Scroll a ScrollViewer vertically by a given offset. /// </summary> /// <param name="viewer">The ScrollViewer.</param> /// <param name="offset">The vertical offset to scroll.</param> private static void ScrollByVerticalOffset(ScrollViewer viewer, double offset) { Debug.Assert(viewer != null, "viewer should not be null!"); offset += viewer.VerticalOffset; offset = CoerceVerticalOffset(viewer, offset); viewer.ScrollToVerticalOffset(offset); }
/// <summary> /// Scroll a ScrollViewer vertically by a given offset. /// </summary> /// <param name="viewer">The ScrollViewer.</param> /// <param name="offset">The vertical offset to scroll.</param> private static void ScrollByVerticalOffset(ScrollViewer viewer, double offset) { Debug.Assert(viewer != null, "viewer should not be null!"); offset += viewer.VerticalOffset; offset = Math.Max(Math.Min(offset, viewer.ExtentHeight), 0); viewer.ScrollToVerticalOffset(offset); }
/// <summary> /// Scroll the ScrollViewer to the top. /// </summary> /// <param name="viewer">The ScrollViewer.</param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="viewer" /> is null. /// </exception> public static void ScrollToTop(this ScrollViewer viewer) { if (viewer == null) { throw new ArgumentNullException("viewer"); } viewer.ScrollToVerticalOffset(0); }
/// <summary> /// Scroll the ScrollViewer to the bottom. /// </summary> /// <param name="viewer">The ScrollViewer.</param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="viewer" /> is null. /// </exception> public static void ScrollToBottom(this ScrollViewer viewer) { if (viewer == null) { throw new ArgumentNullException("viewer"); } viewer.ScrollToVerticalOffset(viewer.ExtentHeight); }
private static void OnVerticalOffsetChanged(DependencyObject target, DependencyPropertyChangedEventArgs e) { System.Windows.Controls.ScrollViewer scrollViewer = target as System.Windows.Controls.ScrollViewer; if (scrollViewer != null) { scrollViewer.ScrollToVerticalOffset((double)e.NewValue); } }
/// <summary> /// VerticalOffsetProperty property changed handler. /// </summary> /// <param name="dependencyObject">ScrollViewer that changed its VerticalOffset.</param> /// <param name="eventArgs">Event arguments.</param> private static void OnVerticalOffsetPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs) { ScrollViewer source = dependencyObject as ScrollViewer; if (source == null) { throw new ArgumentNullException("dependencyObject"); } source.ScrollToVerticalOffset((double)eventArgs.NewValue); }
void OnCursorPositionChanged(object sender, CursorPositionChangedEventArgs args) { if (contentElement == null) { contentElement = GetTemplateChild("ContentElement"); } if (contentElement != null && contentElement is ScrollViewer) { ScrollViewer scrollview = contentElement as ScrollViewer; double offset = scrollview.HorizontalOffset; // Note: for horizontal scrollage, we offset by 1.0 pixel for the width of the cursor ibeam if (args.CursorX < offset) { // need to scroll to the left a bit scrollview.ScrollToHorizontalOffset(Math.Max(args.CursorX - 1.0, 0.0)); } else if (args.CursorX > offset + scrollview.ViewportWidth) { // need to scroll to the right offset = (args.CursorX + 1.0) - scrollview.ViewportWidth; scrollview.ScrollToHorizontalOffset(offset); } offset = scrollview.VerticalOffset; if (args.CursorY < offset) { // need to scroll up a bit scrollview.ScrollToVerticalOffset(args.CursorY); } else if (args.CursorY + args.CursorHeight > offset + scrollview.ViewportHeight) { // need to scroll down a bit offset = (args.CursorY + args.CursorHeight) - Math.Max(args.CursorHeight, scrollview.ViewportHeight); scrollview.ScrollToVerticalOffset(offset); } } }
public void OnScrollChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { Invoke(() => { double old = e.OldValue == null ? 0 : (double)e.OldValue; double a = (double)e.NewValue; if (((double)e.NewValue - old) % 3 == 0) { a = a > old ? old + 1 : old - 1; //获取滚动条位置变化后的属性值 } sv1.ScrollToVerticalOffset(a); sv2.ScrollToVerticalOffset(a); }); }
/// <summary> /// Handles the mouse wheel event. /// </summary> /// <param name="sender">The ScrollViewer.</param> /// <param name="e">Event arguments.</param> private static void OnMouseWheel(object sender, MouseWheelEventArgs e) { ScrollViewer viewer = sender as ScrollViewer; Debug.Assert(viewer != null, "sender should be a non-null ScrollViewer!"); Debug.Assert(e != null, "e should not be null!"); if (!e.Handled) { double position = CoerceVerticalOffset(viewer, viewer.VerticalOffset - e.Delta); viewer.ScrollToVerticalOffset(position); e.Handled = true; } }
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); } }
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 { } } }
public static void ScrollToCenter(ScrollViewer scrollViewer) { scrollViewer.ScrollToHorizontalOffset(scrollViewer.ExtentWidth / 2 - scrollViewer.Width / 2); scrollViewer.ScrollToVerticalOffset(scrollViewer.ExtentHeight / 2 - scrollViewer.Height / 2); }
/// <summary> /// Scroll the ScrollViewer to the bottom. /// </summary> /// <param name="viewer">The ScrollViewer.</param> public static void ScrollToBottom(this ScrollViewer viewer) { Debug.Assert(viewer != null, "viewer should not be null!"); viewer.ScrollToVerticalOffset(viewer.ExtentHeight); }
/// <summary> /// Scroll the ScrollViewer to the top. /// </summary> /// <param name="viewer">The ScrollViewer.</param> public static void ScrollToTop(this ScrollViewer viewer) { Debug.Assert(viewer != null, "viewer should not be null!"); viewer.ScrollToVerticalOffset(0); }
/// <summary> /// Shows log file in a separate child window on top /// </summary> public static void ShowLog() { var popup = new Popup(); var text = new TextBlock(); text.Width = 800; text.TextWrapping = TextWrapping.Wrap; try { text.Text = GetLogContents(); } catch (Exception exception) { text.Text = string.Format("Cannot open log file, exception is\n {0}", exception.ToString()); } var closeButton = new Button(); closeButton.Content = "Close"; closeButton.Click += (s, e) => popup.IsOpen = false; var scroll = new ScrollViewer(); scroll.Background = new SolidColorBrush(Colors.White); scroll.Content = text; scroll.Height = 400; var content = new StackPanel(); content.Children.Add(scroll); content.Children.Add(closeButton); popup.Child = content; popup.IsOpen = true; // scroll to the bottom scroll.UpdateLayout(); scroll.ScrollToVerticalOffset(double.MaxValue); }
/// <summary> /// Scrolls grid content if necessary. /// </summary> /// <param name="yPos">Dragged object position.</param> /// <param name="scrollViewer">Scroll viewer.</param> private void _DoAutoScroll(double yPos, ScrollViewer scrollViewer) { double bottomTolerance = BOTTOM_TOLERANCE; double topTolerance = _rowHeight * 2; Debug.Assert(_rowHeight != 0); // Must be initialized. if (scrollViewer == null) return; if (yPos < topTolerance) // Top of visible list? { // Scroll up. scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset - _rowHeight); } else if (yPos > this.ActualHeight - bottomTolerance) //Bottom of visible list? { //Scroll down. scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + _rowHeight); } }
/// <summary> /// Handles scrolling the specified scroll viewer if the drag event indicates the drag /// operation is nearing the scroll viewers top or bottom boundaries. /// </summary> /// <param name="scrollViewer">The scroll viewer.</param> /// <param name="e"> /// The <see cref="System.Windows.DragEventArgs" /> instance containing the event data. /// </param> private static void HandleDragScrolling(ScrollViewer scrollViewer, DragEventArgs e) { const int threshold = 20; const int offset = 10; var mousePoint = e.GetPosition(scrollViewer); if (mousePoint.Y < threshold) { scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset - offset); } else if (mousePoint.Y > (scrollViewer.ActualHeight - threshold)) { scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + offset); } }
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> /// 滑动改变 /// </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; }
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); }
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 a ScrollViewer vertically by a given offset. /// </summary> /// <param name="viewer">The ScrollViewer.</param> /// <param name="offset">The vertical offset to scroll.</param> private static void ScrollByVerticalOffset(ScrollViewer viewer, double offset) { Debug.Assert(viewer != null, "viewer should not be null!"); offset += viewer.VerticalOffset; offset = CoerceVerticalOffset(viewer, offset); viewer.ScrollToVerticalOffset(offset); }
/// <summary> /// Used when virtualization is on. Moves the viewport so that HandleScrollByPage will work. /// </summary> private void PreScrollByPage(ScrollViewer scroller, bool up) { double startTop, startBottom; // offset of the top and bottom of the selected element from the top of the viewport double adjustmentOffset = 0d; // offset we'll have to move the viewport in order to do the 'classic' page up / down algorithm double distance = 0d; // distance from the furthest edge of either the selected item or the item we're about to select to the nearest edge of the viewport; double viewportHeight = scroller.ViewportHeight; _selectedContainer.GetTopAndBottom(scroller, out startTop, out startBottom); // // What we're doing here: // The TreeView page down algorithm walks the tree looking for the item a page up / down from the currently selected item. // When virtualized that algorithm breaks down since some items aren't generated. // // VSP maintains one page of containers above and below the viewport. // Here we move the viewport so that the selected item or newly selected item, whichever is further, // is only 1 page away from the top or bottom of the viewport. That allows us to use the existing // non-virtualization algorithm. // if (startBottom < 0) { // The selected item is above the viewport; if we page up the furthest distance will be the soon-to-be selected item distance = startBottom; if (up) { distance -= viewportHeight; } } else if (startTop > 0) { // The selected item is below the viewport; if we page down the furthest distance will be the soon-to-be selected item distance = startTop - viewportHeight; if (!up) { distance += viewportHeight; } } if (Math.Abs(distance) > viewportHeight) { // The selected or soon-to-be selected item is more than one page away from the nearest edge of the viewport. // Create the adjustment offset. Distance is delta between the viewport edge and the furthest item; // we have to adjust by the viewport height to have the furthest item 1 page from the viewport adjustmentOffset = distance < 0 ? distance + viewportHeight : distance - viewportHeight; // // Set the offset and update layout to ensure that all containers on the path from the selected item to the // item we want to select are generated. // scroller.ScrollToVerticalOffset(scroller.VerticalOffset + adjustmentOffset); ContextLayoutManager layoutManager = ContextLayoutManager.From(Dispatcher); layoutManager.UpdateLayout(); } }
private static void ScrollByVerticalOffset(ScrollViewer viewer, double offset) { offset += viewer.VerticalOffset; offset = ScrollViewerExtensions.CoerceVerticalOffset(viewer, offset); viewer.ScrollToVerticalOffset(offset); }
/// <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(); } }
/// <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> /// Adds mouse wheel support to a <see cref="ScrollViewer"/>. /// As long as the <see cref="ScrollViewer"/> has focus, /// the mouse wheel can be used to scroll up and down. /// </summary> /// <param name="parentScrollViewer">The parent <see cref="ScrollViewer"/> which contains another <see cref="ScrollViewer" /> which should have mouse wheel scrolling support.</param> /// <param name="childScrollViewer">A child <see cref="ScrollViewer"/> to add mouse wheel scrolling support to.</param> /// <param name="scrollAmount">The amount to scroll by when the mouse wheel is moved.</param> /// <returns>The child <see cref="ScrollViewer"/>.</returns> public static ScrollViewer AddMouseWheelSupport(this ScrollViewer parentScrollViewer, ScrollViewer childScrollViewer, double scrollAmount) { var mouseWheelHelper = new MouseWheelSupport(childScrollViewer, parentScrollViewer); mouseWheelHelper.MouseWheelMoved += (source, eventArgs) => { var delta = eventArgs.WheelDelta; delta *= scrollAmount; var newOffset = childScrollViewer.VerticalOffset - delta; if (newOffset > childScrollViewer.ScrollableHeight) newOffset = childScrollViewer.ScrollableHeight; else if (newOffset < 0) newOffset = 0; childScrollViewer.ScrollToVerticalOffset(newOffset); eventArgs.BrowserEventHandled = true; }; return childScrollViewer; }
/// <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); } }
public static void AppendToActivityLog(ScrollViewer scrollViewer, TextBlock textBlock, MessageLevelsEnum level, string text) { if (scrollViewer.Dispatcher.CheckAccess()) { Run activityRun = new Run(); activityRun.Text = text; activityRun.Foreground = GetBrushForMessageLevel(level); textBlock.Inlines.Add(activityRun); textBlock.Inlines.Add(new LineBreak()); //scrollViewer.ScrollToVerticalOffset(scrollViewer.ScrollableHeight + 20); scrollViewer.UpdateLayout(); scrollViewer.ScrollToVerticalOffset(Double.MaxValue); } else { scrollViewer.Dispatcher.BeginInvoke(new AppendToActivityLogDelegate(AppendToActivityLog), scrollViewer, textBlock, level, text); } }
private static void addToVerticalScrollGroup(string verticalGroupName, ScrollViewer scrollViewer) { if (verticalScrollGroups.ContainsKey(verticalGroupName)) { scrollViewer.ScrollToVerticalOffset(verticalScrollGroups[verticalGroupName].Offset); verticalScrollGroups[verticalGroupName].ScrollViewers.Add(scrollViewer); } else { verticalScrollGroups.Add(verticalGroupName, new OffSetContainer { ScrollViewers = new List<ScrollViewer> { scrollViewer }, Offset = scrollViewer.VerticalOffset }); } scrollViewer.ScrollChanged += ScrollViewer_VerticalScrollChanged; }