Answer the question
In order to leave comments, you need to log in
LongListSelector (or ListBox) scrolling animation?
Hello. The task set to itself the following:
Is LongListSelector. As soon as the data is loaded into it, you need to scroll from the code to a specific element.
I used ScrollTo. The function works, but it seemed to me that this was not enough. I decided to google how to animate this thing beautifully. There is some deprecate function AnimateTo. I tried it - the effect is the same as ScrollTo, no animations.
Further googling gave me the idea to add my own DependencyProperty to the vertical scroll offset:
public partial class LongListSelector : Control
{
public static childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
{
for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is childItem)
return (childItem)child;
else
{
childItem childOfChild = FindVisualChild<childItem>(child);
if (childOfChild != null)
return childOfChild;
}
}
return null;
}
public static readonly DependencyProperty VerticalOffsetProperty = DependencyProperty.Register("VerticalOffset",
typeof(double), typeof(LongListSelector), new PropertyMetadata(VerticalOffsetPropertyChanged));
public double VerticalOffset
{
get { return (double)this.GetValue(VerticalOffsetProperty); }
set { this.SetValue(VerticalOffsetProperty, value); }
}
private static void VerticalOffsetPropertyChanged(object sender, DependencyPropertyChangedEventArgs args)
{
LongListSelector cThis = sender as LongListSelector;
ScrollViewer LScrollViewer = FindVisualChild<ScrollViewer>(cThis);
LScrollViewer.ScrollToVerticalOffset((double)args.NewValue);
}
public static readonly DependencyProperty ContentAreaProperty = DependencyProperty.Register("ContentArea", typeof(object), typeof(LongListSelector), null);
public object ContentArea
{
get { return (object)GetValue(ContentAreaProperty); }
set { SetValue(ContentAreaProperty, value); }
}
...
DoubleAnimation verticalAnimation = new DoubleAnimation();
verticalAnimation.From = 0;
verticalAnimation.To = 25;
verticalAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(5000));
CubicEase ea = new CubicEase();
ea.EasingMode = EasingMode.EaseInOut;
verticalAnimation.EasingFunction = ea;
var sb = new Storyboard();
sb.Children.Add(verticalAnimation);
Storyboard.SetTarget(verticalAnimation, buddies);
Storyboard.SetTargetProperty(verticalAnimation, new PropertyPath(LongListSelector.VerticalOffsetProperty));
sb.Begin();
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question