2009-10-28 86 views
12

是否可以更改WPF ScrollViewer滚动的数量?我只是想知道是否可以更改滚动查看器,以便在使用鼠标滚轮或滚动查看器箭头时可以更改增量滚动的数量。Wpf ScrollViewer Scroll Amount

回答

12

简短的回答是:如果不编写一些自定义滚动代码,没有办法做到这一点,但不要让它吓倒你,它并不是那么难。

ScrollViewer通过使用物理单位(即像素)进行滚动或通过与IScrollInfo实现结合使用来使用逻辑单位。这由设置the CanContentScroll property控制,其中值为false表示“使用物理单位滚动内容”,值true表示“逻辑滚动内容”。

那么ScrollViewer如何在逻辑上滚动内容?通过与IScrollInfo实现进行通信。因此,当某人执行逻辑操作时,您可以如何接管面板内容的滚动情况。 Take a look at the documentation for IScrollInfo以获得可以请求滚动的所有测量逻辑单元的列表,但是由于您提到了鼠标滚轮,因此您将主要对MouseWheelUp/Down/Left/Right方法感兴趣。

0

你可以在scrollviewer上实现一个行为。在我的情况下,CanContentScroll没有工作。下面的解决方案适用于滚动鼠标滚轮以及拖动滚动条。

public class StepSizeBehavior : Behavior<ScrollViewer> 
{ 
    public int StepSize { get; set; } 

    #region Attach & Detach 
    protected override void OnAttached() 
    { 
     CheckHeightModulesStepSize(); 
     AssociatedObject.ScrollChanged += AssociatedObject_ScrollChanged; 
     base.OnAttached(); 
    } 

    protected override void OnDetaching() 
    { 
     AssociatedObject.ScrollChanged -= AssociatedObject_ScrollChanged; 
     base.OnDetaching(); 
    } 
    #endregion 

    [Conditional("DEBUG")] 
    private void CheckHeightModulesStepSize() 
    { 
     var height = AssociatedObject.Height; 
     var remainder = height%StepSize; 
     if (remainder > 0) 
     { 
      throw new ArgumentException($"{nameof(StepSize)} should be set to a value by which the height van be divised without a remainder."); 
     } 
    } 

    private void AssociatedObject_ScrollChanged(object sender, ScrollChangedEventArgs e) 
    { 
     const double stepSize = 62; 
     var scrollViewer = (ScrollViewer)sender; 
     var steps = Math.Round(scrollViewer.VerticalOffset/stepSize, 0); 
     var scrollPosition = steps * stepSize; 
     if (scrollPosition >= scrollViewer.ScrollableHeight) 
     { 
      scrollViewer.ScrollToBottom(); 
      return; 
     } 
     scrollViewer.ScrollToVerticalOffset(scrollPosition); 
    } 
} 

你会使用这样的:

<ScrollViewer MaxHeight="248" 
       VerticalScrollBarVisibility="Auto"> 
    <i:Interaction.Behaviors> 
     <behaviors:StepSizeBehavior StepSize="62" /> 
    </i:Interaction.Behaviors> 
0

我这样做是为了确保scrollbar1.ValueChanged整数:

scrollbar1.Value = Math.Round(scrollbar1.Value, 0, MidpointRounding.AwayFromZero)