Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I don't think you will be able to use a WPF storyboard for the animation because storyboards animate WPF dependency properties. You will need to call <code>ScrollViewer.ScrollToHorizontalOffset(double)</code> to scroll.</p> <p>You could try creating a custom dependency property that calls SetHorizontalOffset in the OnDependencyPropertyChanged() function. Then you could animate this property.</p> <pre><code>public static readonly DependencyProperty ScrollOffsetProperty = DependencyProperty.Register("ScrollOffset", typeof(double), typeof(YOUR_TYPE), new FrameworkPropertyMetadata(0.0, new PropertyChangedCallback(OnScrollOffsetChanged))); public double ScrollOffset { get { return (double)GetValue(ScrollOffsetProperty); } set { SetValue(ScrollOffsetProperty, value); } } private static void OnScrollOffsetChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) { YOUR_TYPE myObj = obj as YOUR_TYPE; if (myObj != null) myObj.SCROLL_VIEWER.ScrollToHorizontalOffset(myObj.ScrollOffset); } </code></pre> <p>To get the scroll viewer you can use the VisualTreeHelper to search the visual children of the ListBox. Save a reference to the ScrollViewer because you will need it later. Try this:</p> <pre><code>public static childItem FindVisualChild&lt;childItem&gt;(DependencyObject obj) where childItem : DependencyObject { // Iterate through all immediate children for (int i = 0; i &lt; VisualTreeHelper.GetChildrenCount(obj); i++) { DependencyObject child = VisualTreeHelper.GetChild(obj, i); if (child != null &amp;&amp; child is childItem) return (childItem)child; else { childItem childOfChild = FindVisualChild&lt;childItem&gt;(child); if (childOfChild != null) return childOfChild; } } return null; } </code></pre> <p>This function returns the first visual child of the parameter type. Call <code>FindVisualChild&lt;ScrollViewer&gt;(ListBox)</code> to get the ScrollViewer.</p> <p>Finally, try using <code>UIElement.TranslatePoint(Point, UIElement)</code> to get the X position of the item. Call this function on the item, pass in 0,0 for the point, and pass in the ScrollViewer.</p> <p>Hope this helps.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload