动画框滚动 [英] Animated Listbox scrolling
问题描述
我有一个垂直的WPF列表框,绑定到一个集合,使用自定义的ItemTemplate。我注意到几件事情:
I have a vertical WPF listbox, bound to a collection, with a custom ItemTemplate. I've noticed a few things:
-
不同于一个独立的ScrollViewer,列表框不连续滚动,但是在增量等于项目的高度跳跃。凡在XAML是这种差异定义(列表框模板只包含毕竟一个ScrollViewer中?
Unlike a standalone scrollviewer, the listbox does not scroll continuously, but jumps in increments equal to the height of the items. Where in XAML is this difference defined (the listbox template just contains a scrollviewer after all?
是否有可能进行动画此跳?我想这个动画,使其迅速从动画一个偏移到另一个跳。锦上添花将是动画,显示在最后一些INTERIA和摆动。我希望会有此一pre定义的行为,但似乎不是之一。在code。与BeginAnimation这样做我自己背后尝试都失败了,跟抛出告诉我的ScrollViewer的VerticalOffset属性无法进行动画处理的异常。
Is it possible to animate this jump? I'm trying to animate this so that it quickly animates the jump from one offset to another. Icing on the cake would be for the animation to display some interia and 'wobble' at the end. I was hoping there would be a pre defined Behavior for this, but there seems not to be one. Attempts at doing this myself in code behind with BeginAnimation have failed, with an Exception thrown telling me that the VerticalOffset property of Scrollviewer cannot be animated.
任何线索?
谢谢
汤姆
推荐答案
1 - 如果你想不滚动跳,你可以改变ScrollViewer.CanContentScroll为False。
1- If you want scroll not to jump, you can change ScrollViewer.CanContentScroll to False.
<ListBox ScrollViewer.CanContentScroll="False" .... >.....</ListBox>
2 - 你可以创建你自己的控制,能够以动画它的偏移量。
2- You can create you own control that is able to animate it's offset.
MyScrollViewer类
MyScrollViewer Class
public class MyScrollViewer : ScrollViewer
{
public static readonly DependencyProperty MyOffsetProperty = DependencyProperty.Register(
"MyOffset", typeof(double), typeof(MyScrollViewer),
new PropertyMetadata(new PropertyChangedCallback(onChanged)));
public double MyOffset
{
get { return (double)this.GetValue(ScrollViewer.VerticalOffsetProperty); }
set { this.ScrollToVerticalOffset(value); }
}
private static void onChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((MyScrollViewer)d).MyOffset = (double)e.NewValue;
}
}
在MainWindow.xaml什么
in MainWindow.xaml or something
<Grid>
<my:MyScrollViewer x:Name="myScroll">
<ListBox x:Name="myList">
</ListBox>
</my:MyScrollViewer>
<Button Content="Down"
VerticalAlignment="Bottom"
HorizontalAlignment="Right"
Margin="10"
Click="Button_Click" />
</Grid>
MainWindow.cs
MainWindow.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
for (int i = 1; i < 50; i++)
myList.Items.Add(i);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
double offset = (double)myScroll.GetValue(MyScrollViewer.MyOffsetProperty);
DoubleAnimation goDown = new DoubleAnimation(
offset,
offset + 100,
new Duration(TimeSpan.FromSeconds(2)));
myScroll.BeginAnimation(MyScrollViewer.MyOffsetProperty, goDown);
}
}
这篇关于动画框滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!