Page 1 of 1
Preventing a Listboxitem from getting selected
Posted: 10 Mar 2014, 15:53
by Andreas Schooll
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
Re: Preventing a Listboxitem from getting selected
Posted: 11 Mar 2014, 23:29
by sfernandez
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.
Re: Preventing a Listboxitem from getting selected
Posted: 12 Mar 2014, 16:05
by Andreas Schooll
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
Re: Preventing a Listboxitem from getting selected
Posted: 14 Mar 2014, 17:16
by sfernandez
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).
Re: Preventing a Listboxitem from getting selected
Posted: 14 Mar 2014, 18:45
by jsantos
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?
Re: Preventing a Listboxitem from getting selected
Posted: 17 Mar 2014, 15:04
by Andreas Schooll
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.
Re: Preventing a Listboxitem from getting selected
Posted: 17 Mar 2014, 16:52
by jsantos
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.