WP7 Listbox - clearing selected item - windows-phone-7

I am using a listbox in a data template - and from an earlier post I cannot reference the listbox directly in the code behind.
As a result I am capturing the last selected object in the selectionchanged event for the listbox and using this when I want to navigate.
I now need to also clear the selected object in the listbox -can I do this in the selectionchanged event (after storing it away).
Alternatively I could use the MouseLeftButtonDown event on the listbox (which I understand is the equivalent of a 'click') but can I get the selected object in the listbox in this event.
thanks

In the selection changed event set <ListboxName>.SelectedIndex = -1;
Also, do not use the MouseLeftButtonDown event. This will fire whenever the user touches the ListBox, even if they're just trying to scroll up / down and not actually selecting an item.

If you can't change the SelectedIndex in code behind then, instead of detecting the SelectionChanged event you could detect a Tap event on the ListBoxItem.

Related

How to deselect lisboxitem in mvvmlight?

I had a quick question on working with listboxes in WP7 using MVVM Light. Basically before I was using MVVM all I had to do was set the SelectedIndex to -1 inside of the OnNavigatedTo event when the page was navigated to. Then inside of the SelectionChanged event I would check if the SelectedIndex was equal to -1 and if so I would ignore it. The reason I did this was just in case the user wanted to select the same item again when they came back to the page.
Now with MVVM (MVVM Light) I'm binding the event to a command, which takes care of the first part. But now I'm stuck because I don't know how to set the SelectedIndex to -1 from the ViewModel which prevents the user from selecting the same item again. Any ideas?
An even better solution is to not use the selection event to trigger a navigation. Use the tap event of the indivdual item instead.
This also avoids issues of accidental navigation when scrolling.
Try creating a SelectedItem property on your viewmodel, then do a two-way between the SelectedItem viewmodel property and the SelectedItem property on your ListBox. Then you can update it with whatever value you want when your Command fires and the result will propagate back to your ListBox.

ListBox SelectedIndex is always -1

I have a Listbox with Button and Textblock as ItemTemplate. Button has an onClick event.
All i want is to get the index of the clicked item, but SelectedIndex property in my ListBox is always -1!
How can I get the index of the clicked item?
The problem is that the Button control is swallowing the mouse events to provide the Click behavior, so the ListBox never receives any events to tell it that the selection has changed.
As #Alexander suggests, you could use an MVVM-style approach with commands to handle the action in a view model and pass the data context as the parameter.
Alternatively, you could replace the Button with any other layout control and either use the gesture service from the Silverlight Toolkit or use the regular MouseLeftButtonUp event. In either instance, the mouse events will bubble up and enable the ListBox to handle selection.
You might want to actually select something in your listbox then :p
If you want to get data object associated with current ListItem use RouteCommand. Here is example
<ListView.ItemTemplate>
<DataTemplate>
<Button Margin="5,0" Command="{StaticResource ButtonCommand}"
CommandParameter="{Binding}">
</DataTemplate>
<ListView.ItemTemplate>
You need also define Command at least in ListView.Resources
<RoutedCommand x:Key="ButtonCommand"/>
And then setup CommandBinding like this
<ListView.CommandBindings>
<CommandBinding Command="{StaticResource ButtonCommand}" Executed="Button_Click"/>
</ListView.CommandBindings>
In the end write handler
Button_Click(object sender, ExecutedRoutedEventArgs e)
{
e.Parameter// object that you Specified in CommandParameter
}
In advance you can use any MVVM framework, define all command and commands logic in model and then just bind this commands to corresponding elements.

Is it possible to know when a particular panorama item is displayed

I have a list of panorama Items.
I want to know when the user has reached a particular panorama item and then take necessary action.
I tried using the eventhandler GotFocus on the panorama item but it doesn't get invoked when the user reaches the item
The Panorama control has a SelectionChanged event which is fired when the user pans from one item to the next. You can then use the SelectedItem or SelectedIndex properties to find the item which is currently in view.

windows phone 7 button in longlistselector

I've read somewhere that longlistselector from the toolkit is better in performance than the existing listbox. So, I changed the listbox to longlistselector. Now I have a image button control to keep in the longlistselector (that acts like a checkbox). When I click on the button, the list selection changed event is fired along with the button click. The button in listbox works fine as expected but not in longlistselector. How can I stop the list selection changed event? I searched a lot on this but couldn't find anything useful. First of all is it possible?
I wouldn't take it for granted that the long list selector performs better than the listbox. The listbox uses a virtualizing stack panel when binding is involved and is pretty performant. I went down the road of using the list picker from the toolkit and ended up regretting it due to some bad performance problems. If it works with the listbox I'd say stick with the listbox and only move away if you find you have perfomance issues in the future.
When a button is clicked a button event handler is triggered and when a item in the long list selector is changed the corresponding selection changed event is triggered IF it is also registered. But the button is clicked on the same selected item, only the button event handler is triggered. I suggest to have only a button event handler and get the selected item from it.
private void ButtonEvent_Click(object sender, RoutedEventArgs e)
{
HoldingClass clicked=((sender as Button).DataContext as HoldingClass);
//Do something with the HoldingClass as this is the binding element to the long list selector
}
Change the ClickMode to Press in XAML
ClickMode="Press"
and inside you Click event handler make (YourListName).SelectedItem = null;
private void deleteButton_Click(object sender, RoutedEventArgs e)
{
MainLongListSelector.SelectedItem = null;
..
}

How to detect a Pivot item is selected in Windows Phone 7

I am implementing the Pivot control in Windows Phone 7.
I want to know which Pivot item is selected and active after the user does a swipe gesture.
How to detect the swipe gesture event related to Pivot control?
Which delegate method should I use ?
SelectionChanged is the event to wire up to for detecting Pivot item changes.
As kP suggests, SelectedIndex/SelectedItem are the properties you can access to see which item is currently selected.
Here's an example event handler to show the SelectedIndex each time it changes.
private void thisPivot_SelectionChanged(object sender, SelectionChangedEventArgs e) {
System.Diagnostics.Debug.WriteLine(thisPivot.SelectedIndex);
}
You could use the Pivot.SelectedItem method which is a getter and setter. This will allow you to return the current PivotItem the user is on. Alternatively, you could use the Pivot.SelectedIndex method if you just want to access the indexes of each PivotItem.
If you want to perform an action once the PivotItem has actually finished loading entirely and transition animation sequence is complete, take a look at the LoadedPivotItem event.

Resources