Andreas Schooll
Topic Author
Posts: 25
Joined: 30 Sep 2013, 16:58

Preventing a Listboxitem from getting selected

10 Mar 2014, 15:53

Hi,

i have a question related to ListBox / ListViews.

We are catching the MouseEvents in our lists ScrollViewers to do customized scrolling.
ScrollView.PreviewMouseDown += OnTouch;
ScrollView.PreviewMouseUp += OnTouchLost;
ScrollView.PreviewMouseMove += OnTouchMove;

Unfortunately if you scroll around the items in the lists will get selected. This happens on mousedown and in mousemove. We would like to select the items on MouseUp only if no scrolling has happened before. Is there a way to dispose the MouseEvents so the listItems will not receive them?

best regards,
Andreas
 
User avatar
sfernandez
Site Admin
Posts: 1970
Joined: 22 Dec 2011, 19:20

Re: Preventing a Listboxitem from getting selected

11 Mar 2014, 23:29

Hi,

ListBoxItems get selected responding to MouseLeftButtonDown and MouseRightButtonDown events. You should try handling these events by setting args.handled = true.

Other solution could be to handle Touch events so touches don't get promoted to Mouse events.
 
Andreas Schooll
Topic Author
Posts: 25
Joined: 30 Sep 2013, 16:58

Re: Preventing a Listboxitem from getting selected

12 Mar 2014, 16:05

Hi,

thanks for your response. I have tried to prevent the selection by setting the args.handled to true.
This works to prevent the selection on button down, but the items still get selected if i move the mouse (i.e. with left button still down) over another item. Is there a way to prevent this also?

More questions related to the first:
Is there a way to determine what item would have been selected in the MouseButtonDown events to select it later in the mouse button up event?

Can i add eventhandlers to the listviewitem to catch events directly inside the items?
Can i derive listviewitem class to customize it?

best regards,
Andreas
 
User avatar
sfernandez
Site Admin
Posts: 1970
Joined: 22 Dec 2011, 19:20

Re: Preventing a Listboxitem from getting selected

14 Mar 2014, 17:16

The items still get selected if i move the mouse (i.e. with left button still down) over another item. Is there a way to prevent this also?
The other event used by ListBoxItem to detect selection is MouseEnter, and that event is a Direct routed event, so you cannot cancel it before the control's own event handler is called.
Is there a way to determine what item would have been selected in the MouseButtonDown events to select it later in the mouse button up event?
You can use Hit Testing to determine which item would have been selected.
ListBoxItem FindItemUnderMouse(Visual rootElement, Point mousePosition)
{
    HitTestResult hr = VisualTreeHelper.HitTest(rootElement, mousePosition);
    Visual v = hr.visualHit;
    while (v != null)
    {
        var item = v.As<ListBoxItem>();
        if (item != null)
        {
            // item found
            return item;
        }

        v = VisualTreeHelper.GetParent(v);
    }

    // item not found
    return null
}
Can i add eventhandlers to the listviewitem to catch events directly inside the items?
Yes, you will only need a reference to the ListBoxItem.
Can i derive listviewitem class to customize it?
In C++ you have full support to inherit from any control and customize it. But in Unity control extension support is more limited (for example, you can't override event handlers).
 
User avatar
jsantos
Site Admin
Posts: 2948
Joined: 20 Jan 2012, 17:18
Contact:

Re: Preventing a Listboxitem from getting selected

14 Mar 2014, 18:45

We are catching the MouseEvents in our lists ScrollViewers to do customized scrolling.
May I ask you what kind of customized scrolling are you implementing? Is this for emulating touch events?
 
Andreas Schooll
Topic Author
Posts: 25
Joined: 30 Sep 2013, 16:58

Re: Preventing a Listboxitem from getting selected

17 Mar 2014, 15:04

Thanks for your respone. I will look into the details later and see if we can get something out of that.

Yes, it is to emulate touch events and to implement a touch scrolling with deceleration. We implemented this before the actual touch mechanism of scrollviewers got implemented. Currently we (and our customer) like the fact, that the scrolling behaviour works for the desktop and mobile version.
 
User avatar
jsantos
Site Admin
Posts: 2948
Joined: 20 Jan 2012, 17:18
Contact:

Re: Preventing a Listboxitem from getting selected

17 Mar 2014, 16:52

Another alternative is that you enable the default touch behavior of the scrollviewer (intertia is also implemented) and redirect Mouse events to Touch events with the IRenderer.

In fact, we use this emulation here when we want to test Touch events in windows machines without Touch Screen. We should probably make this emulation available to everybody.

Who is online

Users browsing this forum: Tiddan and 1 guest