我找到了解决方案。
据我所知,这不是填充和边距的问题。
我所做的是为 ScrollViewer 创建一个行为,我在其中处理 ScroolChanged。
行为代码:
class ScrollViewerRestrictedScrollBehavior : Behavior<ScrollViewer>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.ScrollChanged += AssociatedObject_ScrollChanged;
}
void AssociatedObject_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
if (e.VerticalOffset >= MaxItemCount)
{
AssociatedObject.ScrollToVerticalOffset(JumpToItemCount);
}
}
public double MaxItemCount
{
get { return (double)GetValue(MaxItemCountProperty); }
set { SetValue(MaxItemCountProperty, value); }
}
// Using a DependencyProperty as the backing store for MaxItemCount. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MaxItemCountProperty =
DependencyProperty.Register("MaxItemCount", typeof(double), typeof(ScrollViewerRestrictedScrollBehavior), new UIPropertyMetadata(0d));
public double JumpToItemCount
{
get { return (double)GetValue(JumpToItemCountProperty); }
set { SetValue(JumpToItemCountProperty, value); }
}
// Using a DependencyProperty as the backing store for JumpToItemCount. This enables animation, styling, binding, etc...
public static readonly DependencyProperty JumpToItemCountProperty =
DependencyProperty.Register("JumpToItemCount", typeof(double), typeof(ScrollViewerRestrictedScrollBehavior), new UIPropertyMetadata(0d));
}
使用它的 xaml 代码(注意我使用的值非常接近 6,因此在垂直滚动条中看不到小跳) - 这是 ListBox 样式的一部分:
<ScrollViewer Focusable="false" Padding="{TemplateBinding Padding}"
Template="{StaticResource landmarksScrollViewerControlTemplate}" >
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
<i:Interaction.Behaviors>
<ModuleBehaviors:ScrollViewerRestrictedScrollBehavior MaxItemCount="5.99" JumpToItemCount="5.97" />
</i:Interaction.Behaviors>
</ScrollViewer>