简体   繁体   中英

How to stop WP7 ListBox auto scrolling clicked item into view?

I have an application that has a nice flyout animation when the users selects an item in the listbox. When the user clicks the back button, a flyin animation is used. You can see the example here : I am using the same animation code as this sample. everything works fine until the clicked item is partially off the bottom or top of the screen. The flyout animation will happen properly but when the user returns to the list, the listbox auto scrolls the selected item up (or down) to be fully in view. The flyin animation however returns the text to the original (before auto scroll happened) location. You may need to download the working sample to fully see what I am on about.

The question I have is - is there a way to disable this auto-scroll action. The built in apps like messaging and email don't scroll a partially visible item into view when selected.

Thanks

I don't know how to disable auto-scroll action, but I have a quick fix for that code:

In class ItemFlyInAndOutAnimations

add a field

private FrameworkElement _element; //let's consider the line is 266

in public void ItemFlyIn() make changes:

public void ItemFlyIn()
{
  if (_popupCanvas.Children.Count != 2)
    return;

  _popup.IsOpen = true;
  _backgroundMask.Opacity = 0.0;

  Image animatedImage = _popupCanvas.Children[1] as Image;

  var sb = new Storyboard();

  var rootFame =  Application.Current.RootVisual as FrameworkElement; //new line
  var targetElementPosition = _element.GetRelativePosition(rootFame); //new line
  // animate the X position
  var db = CreateDoubleAnimation(targetElementPosition.X - 100, targetElementPosition.X,
      new SineEase(),
      _targetElementClone, Canvas.LeftProperty, _flyInSpeed); //reference changed!
  sb.Children.Add(db);

  // animate the Y position
  db = CreateDoubleAnimation(targetElementPosition.Y - 50, targetElementPosition.Y,
      new SineEase(),
      _targetElementClone, Canvas.TopProperty, _flyInSpeed); //reference changed!
  sb.Children.Add(db);
  //other code is the same

in public void ItemFlyOut(FrameworkElement element, Action action)

after this line

_targetElementPosition = element.GetRelativePosition(rootElement);

add this one:

_element = element;

What have I done:

In this code I save the reference to animated UI element and update it's position on back animation.

You'd better to test this code, but it seems to be ok.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM