ScrollIntoView不适用于Touch

时间:2015-05-01 15:34:26

标签: c# scroll listbox touch pushpin

所以我的地图上有一些PushPins,右边的列表列出了所有PushPins(使用相同的视图模型)

当我点击地图上的图钉时,我想将项目滚动到列表中的视图中。

我目前在Click和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); 
}

点击:

void pp_MouseDown(object sender, MouseButtonEventArgs e)
{
    ScrollPushPin(sender as PushPin);
}

触摸:

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);
    }
}

虽然这个代码适用于我用鼠标点击我的图钉时,Touch事件不会将我的PushPin滚动到视图中,我看不出原因?

我也尝试过:

this.MyList.Dispatcher.Invoke((Action)(() => this.MyList.ScrollIntoView(pushpin)));

this.MyList.ScrollIntoView(this.MyList.Items[val]);

1 个答案:

答案 0 :(得分:0)

所以不要问我为什么会这样,但在事件结束时添加e.Handled = true可以解决问题:

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
}

修改

添加e.Handled = true会导致更多问题,所以我决定编写自己的ScrollIntoView

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;
}