Listbox item is not getting fired - windows-phone-7

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;
}
}

Related

List item unresponsive on second click event

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;
}

WP7 : Populating a ListBox depending on the input from another ListBox

I'm trying to add ListItems to a ListBox (ListBox3) depending on the selectedItem of another ListBox (ListBox1). The problem is , The Items aren't added to the Listbox.
Here's the code :
private void createlist()
{
if (listBox1.SelectedValue.ToString().Equals("EPL"))
{
ListBoxItem manchesterunited = new ListBoxItem();
manchesterunited.Content = "Manchester United";
listBox3.Items.Add(manchesterunited);
}
}
private void listBox1_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
createlist();
}
createlist() does the changes and is called in the SelctionChanged() event of ListBox1.
New to C# and WP7 programming , any help will be much appreciated.
Create the lists in your viewmodel and bind the listbox to a list<> in your viewmodel say SelectedList. When the user selects the item from ListBox1 just change the value of SelectedList with the appropriate List and Notify the property changed event. And it will be done.!
i think you program not run in mvvm structure.
make sure your logic is right. you can make a breakpoint at the line
ListBoxItem manchesterunited = new ListBoxItem();
Ensure run those code in if code block.
the way add a control in a listbox is correct.

How can I set a property for AutoMouseScroll and Mouse hower property to UltraDropdown?

I am using UltraDropDown control to bind a column of one UltraGrid control to list People category in list format.
This drop-down control have more than 25 items and show 8 categories Max, it's very fine. Now whenever I click on drop-down control to see all people category list then i have to hold scroll bar and drag down to see all categories. but I want to show all categories when I mouse scroll and its automatically move up and down to show all, and one more thing I want, when i mouse hover on listed categories then hover item should be shaded or colored.
Please help on both topic.
Thanks & Regards,
Shashi Bhushan Jaiswal
I believe that the first requirement is by default as behavior. Are you handling the MouseWheel event for this to not work?
Here is the code for your second requirement but I do not know if it is a good approach to use the MouseHover event as you like but this is your requirement:
void ultraDropDown1_MouseHover(object sender, EventArgs e)
{
if (cell != null && isInItem) {cell.Cell.Appearance.BackColor = Color.Red;}
}
CellUIElement cell;
bool isInItem = false;
private void ultraDropDown1_MouseEnterElement(object sender, Infragistics.Win.UIElementEventArgs e)
{
if (e.Element is EditorWithTextDisplayTextUIElement && e.Element.Parent.Parent is CellUIElement)
{
cell = (CellUIElement)e.Element.Parent.Parent;
isInItem = true;
}
else isInItem = false;
}

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?

Code to save on row change using bindingNavigator, bindingSource

When using a bindingNavigator and bindingSource and clicking a move button or add button or delete button, the bindingSource completes its action code before the click handler of the button (i.e. user code)
This prevents a save action on the row change. I'd like to find a bindingSource hook, something like 'beforeRowChange'.
I can subclass the bindingSource and get ahead of the add or remove event but that doesn't cover all the row move actions.
Any clues, suggestions welcome.
The BindingNavigator has a property called 'DeleteItem'.
Change this property from 'BindingNavigatorDeleteItem' to '(none)'.
private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
{
if ( bindingSource.Count > 0 )
{
if (MessageBox.Show("Confirm Delete?", "Warning", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
{
bindingSource.RemoveCurrent();
}
}
}

Resources