List item unresponsive on second click event - windows-phone-7

In my Windows Phone 7 application I have a number of list views. Each listViewItem has a click event (By setting 'selectionChanged' attribute to the listBox in my xaml). Now a very peculiar thing happens:
When I click on an item in the listbox the first time everything goes well, in this case the user gets taken to another screen. When I go back from that screen to the listbox, I select the very same listboxitem but this time the event doesn't register, nothing happens...
I then first have to tap on another item, let that ones even fire, then only can I tap on the first item. So in other words, I can't fire a click event for a listItem twice in a row. I'm thinking it's because the event handler on the listbox says 'onSelectionChanged', if you select the same item the selection hasn't technically changed.
So what other eventHandling attribute can I use on my listbox to register selection events on it's items?
Thanks for any help!

AFAIK, Theres is no such event. So, the work around is,
In the OnNavigatedTo event handler of the first page, set the SelectedIndex to -1
YourListBox.SelectedIndex = -1;
And while doing so, make one small modification to your Selection_Changed handler
void Selection_Changed(...)
{
if(YourListBox.SelectedIndex == -1)
return;
//rest of your code
}

In your case, SelectedItem in the ListBox is set for the first time. The second time you tap on the same item, technically its not a SelectionChanged event, hence its not firing.
Clearing the SelectedItem at the end of SelectionChanged event would do the trick.
Below is a code snippet that could be helpful,
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//
// do your stuff here
//
//reset the selection of the sender (ListBox)
(sender as ListBox).SelectedItem = null;
}

Related

Listbox item is not getting fired

I am using listbox inside panorama, like if panorama has 5 items then each panorama item contains a listbox.
The listitem is not getting fired at times, mainly for the second time. For the first them when the list item is clicked then it navigates to next page. When i come back and again i tap on the listitem is not getting fired.
Am using SelectionChanged for list click listener.
I have got a suggestion from web search to use stackpannel instead of grid in , but in some places am unable to use stack pannel because of the component arrangement.
Please suggest me does changing to stackpannel is the only way or is there any other solution for this.
Any kind of ideas are welcome.
When one item is selected in a ListBox, it keeps the record of the selectedindex. When the same element is tapped again, there is no change in the selectedindex and hence the SelectionChanged is not fired. Hence what you can do is setting the selectedindex back to -1 after each selection or after the back navigation to the listbox page
//In the onnavigatedto function, set the listbox selectedindex to -1
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
MyListBox.SelectedIndex = -1;
}
And modify your selectionchanged event like this
private void MyListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//let our code run only if index is not -1
if (MyListBox.SelectedIndex != -1)
{
//Your selectionchanged code
}
}
Hope this helps
UPDATE: For your Panorama case
private void MyListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox listbox = (sender as ListBox);
//let our code run only if index is not -1
if (listbox.SelectedIndex != -1)
{
//Your selectionchanged code
At the end of it set the index back to -1
listbox.SelectedIndex = -1;
}
}

Make a difference between a tab event and a flick event on listbox

I have a listbox which contains a list of images; I don't know how to differentiate betwwen a Flick event and a Tap event, to make a zoom on the chosen image?
There is a Tap event on all elements (in Mango). Tap event don't raised when user scrolls a list.
Also, you can place an image inside a retemplated Button (leave only content holder). Then you get for free Click event and Tilt Effect as well
There is additional support for detecting touch in the XNA library. Trying adding the Microsoft.Xna.Framework.Input.Touch reference to your project
Include the following using statement:
using Microsoft.Xna.Framework.Input.Touch;
Subscribe to the required events in your constructor as follows:
TouchPanel.EnabledGestures = GestureType.Tap | GestureType.Flick;
On your list box create an event for Manipulation Completed as follows:
ManipulationCompleted="ListBoxDays_ManipulationCompleted"
You could add code to the that event method to track the type of events that have been completed with the following code:
private void ListBoxDays_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
{
while (TouchPanel.IsGestureAvailable)
{
GestureSample gesture = TouchPanel.ReadGesture();
if (gesture.GestureType == GestureType.Tap)
{
//Do something
}
if (gesture.GestureType == GestureType.Flick)
{
//Do something else
}
}
}
Hope this Helps

Windows Phone 7 Selection_Changed automatically

currently I'm developing an app for WP7 but came across a little problem with a Listbox event call Selection_Change. The problem is that when i return to the page that contains the listbox the selection_change event triggers without being changed at all or without any user input. The listbox code is similar to this:
private void lsbHistory_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int index = lsbHistory.SelectedIndex;
NavigationService.Navigate(new Uri("/Views/NextPage, UriKind.Relative));
}
On the page I navigate to, the only way out of the navigated page is by pressing back button or start button meaning that it will return to the page that contains the listbox. When I Navigate back the selection change triggers leading me sometimes to a exception. Has anyone been through this before?
Consider always checking if it's -1 (the default value).
private void lsbHistory_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int index = lsbHistory.SelectedIndex;
if (index != -1)
{
NavigationService.Navigate(new Uri("/Views/NextPage, UriKind.Relative));
lsbHistory.SelectedIndex = -1; // Set it to -1, to enable re-selection.
}
}
Also, you should consider wrapping the Navigate call in Dispatcher.BeginInvoke to have a better, more smooth, page transition.
The event will be fired when the list is populated.
The simplest solution for you will probably be to add a check that there is nothing selected before triggering your navigation:
if (lsbHistory.SelectedIndex > -1)
{
// do navigation
}
One thing to notice is that when you navigate back to the page which containt the ListBox, the ListBox still has the SelectedItem set to the value it had when the user navigated away. This means that lsbHistory.SelectedIndex will get the index of the item which was selected when the user navigated forward.
Maybe there's something in your code which presumes that the ListBox's SelectedItem is null when the user navigates to the page?

Unselecting a WP7 listbox item on click

I've seen similar questions asked about WPF, but none of the proposed solutions seem to work under Windows Phone 7.
Basically, I've got a listbox where the behavior needs to be
1) when user taps an item in the list, it's selected.
2) when user taps any other item, the first is unselected and the tapped item is selected (so far this is just normal single select list box behavior)
3) when user taps an already selected item, the item "unselects" (so that there is no selected item at all anymore).
It's certainly easy enough to intercept the MouseLeftButtonDown event and clear the selection, but the ui system appears to continue processing the tab and turns around an reselects the item I've just unselected.
At first, I thought binding could be the problem, and the list items +are+ bound to an observableCollection, but neither the "selectedItem" or "selectedIndex" are bound at all.
I tried setting the event args handled prop to true:
e.Handled = true
but no change.
Any ideas?
Use MouseLeftButtonUp() rather than MouseLeftButtonDown().
private object _selected;
private void ListBox_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
var list = (ListBox) sender;
if (list.SelectedItem == _selected)
{
list.SelectedIndex = -1;
_selected = null;
}
else
{
_selected = list.SelectedItem;
}
}

What is the right event to capture a "Click" in a ListBox on Windows Phone 7?

I'm writing a Windows Phone 7 app with a ListBox. When an item in the ListBox is "clicked" or "hit" with a finger, what is the right event to trap that?
I have tried "SelectedIndexChanged" but that seems to fire on GoBack() when the app is TombStoned and an index of 0 is passed in (which seems odd).
I'm currently using MouseUp which seems to do the trick. But I'm not sure if that's correct.
Note: I discovered the reason SelectionChanged was firing when clicking back. When the Constructor for my Page was firing, and the ItemSouce for my ListBox was being set (databound) that would select the first item in the list and fire the SelectionChanved event. Since this was not initiated by user action, I solved this by simply creating an IsLoaded boolean and setting it to true after setting the ItemSource in the constructor and then checking for that in the event.
If you want to be notified when an item is selected you should catch the SelectionChanged event.
In the handler you check that e.AddedItems contains exactly one item:
void SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count == 1)
{
// .. do something
}
}
On GoBack() you probably have items in the e.RemovedItems collection but nothing in e.AddedItems.

Resources