Domanda

I'm trying to use a WPF listbox on a tablet. In a dummy project I just made a listbox with a lot of items and when i select one this item will be showed in a textblock.

I have a selectionchanged event on the listBox

On my laptop everything works the way it should but when i run it on a tablet the selectionchanged event isn't triggered sporadically. On the screen the old selected item stays selected and the new selected one is highlighted but the item isn't shown in the textblock.

With remote debugging I have seen that the TouchDown, TouchMove and TouchUp event are all triggered, but some times the selectionChanged isn't triggered.

these things I've tried as well: setting in Xaml inside the listbox:

ScrollViewer.PanningMode="None"

When I do this the selectionchanged event is always triggered but the user can't scroll down anymore with swiping (Which must be possible. I think here lies the problem somewhere, but I don't have any solution yet.

Help Needed.

È stato utile?

Soluzione

After a long time a solution for this problem was found. first of we need some variables

private TouchPoint _movePoint;
private double _minimum = 0;
private double _maximum;

Me need to catch the TouchMove event of the listBox. This event triggers many times. We need get maximum and minimum Y-values of were the touch has been.

private void myListBox_TouchMove(object sender, TouchEventArgs e)
{
    _movePoint := e.GetTouchPoint(myListBox);
if (_minimum.Equals(0))
{
        _minimum := _movePoint.Position.Y;
        _maximum := _movePoint.Position.Y;
        return;
}

if (_movePoint.Position.Y < _minimum) 
_minimum := _movePoint.Position.Y;
if (_movePoint.Position.Y > _maximum) 
    _maximum := _movePoint.Position.Y;
}

Now in the TouchUp event we look at the how far have been slided in the vertical direction. If this is not to big (in this example lower then 20), we gonna look at where the touchup event took place and look for the ListBoxItem that is on that place and set IsSelected=ture on this item.

private void myListBox_TouchUp(object sender, TouchEventArgs e)
{
    var difference = _maximum - _minimum;
    _maximum = 0;
    _minimum=0;
    if(difference < 20)
{
  var touchPosition = e.GetTouchPoint(myListBox)  
  UIElement elem = myListBox.InputHitTest(touchPosition.Position) as UIElement;

    while (elem != null)
    {
        if (elem == myListBox)
            return;
        ListBoxItem item = elem as ListBoxItem;
        if (item != null)
        {
            item.IsSelected = true;
            return;
        }
        elem = VisualTreeHelper.GetParent(elem) as UIElement;
    }
}
}

This should work.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top