ScrollIntoView not working with Touch
So, I have a map with a number PushPins
on it and a list on the right side listing everything PushPins
(using the same view models)
When I click a pin on the map, I want to scroll through the item in a list view.
I am currently using this code in Click and OnTouch:
private void ScrollPushPin(Pushpin pushpin)
{
ScrollViewer scrollViewer = GetScrollViewer(MyList) as ScrollViewer;
scrollViewer.ScrollToBottom();
var index = this.MyList.Items.IndexOf(pushpin);
//index is never -1 so I would expect it to work?
this.MyList.ScrollIntoView(pushpin);
}
On click:
void pp_MouseDown(object sender, MouseButtonEventArgs e)
{
ScrollPushPin(sender as PushPin);
}
In touch:
void pp_TouchDown(object sender, TouchEventArgs e)
{
var pushpin = (Pushpin)sender;
pushpin.CaptureTouch(e.TouchDevice);
}
void pp_TouchUp(object sender, TouchEventArgs e)
{
var pushpin = (Pushpin)sender;
if (pushpin != null && e.TouchDevice.Captured == pushpin)
{
pushpin.ReleaseTouchCapture(e.TouchDevice);
ScrollPushPin(pushpin);
}
}
So far this code works fine, when I click my mouse button with my mouse, the Touch events do not scroll my PushPin and I don't understand why?
I've also tried:
this.MyList.Dispatcher.Invoke((Action)(() => this.MyList.ScrollIntoView(pushpin)));
and
this.MyList.ScrollIntoView(this.MyList.Items[val]);
source to share
So don't ask me why this works, but adding e.Handled = true
at the end of my events solved the problem:
void pp_TouchDown(object sender, TouchEventArgs e)
{
var pushpin = (Pushpin)sender;
pushpin.CaptureTouch(e.TouchDevice);
e.Handled = true
}
void pp_TouchUp(object sender, TouchEventArgs e)
{
var pushpin = (Pushpin)sender;
if (pushpin != null && e.TouchDevice.Captured == pushpin)
{
pushpin.ReleaseTouchCapture(e.TouchDevice);
ScrollPushPin(pushpin);
}
e.Handled = true
}
EDIT
the addition e.Handled = true
caused more problems down the line, so I decided to write my ownScrollIntoView
var val = this.MyList.Items.IndexOf(myObj);
if (val != -1)
{
ScrollViewer scrollViewer = GetScrollViewer(MyList) as ScrollViewer;
var itemHeight = scrollViewer.ExtentHeight / this.MyList.Items.Count;
scrollViewer.ScrollToVerticalOffset(itemHeight * val);
}
//where
public static DependencyObject GetScrollViewer(DependencyObject o)
{
if (o is ScrollViewer)
{ return o; }
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(o); i++)
{
var child = VisualTreeHelper.GetChild(o, i);
var result = GetScrollViewer(child);
if (result == null)
{
continue;
}
else
{
return result;
}
}
return null;
}
source to share