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?
Related
We have a tool that is being integrated into our application. We have some strict borders around us too in that we cannot modify the application except for our extensions. I have searched here, I've searched the internet, but cannot find any postings about this problem.
I have a Windows Form that contains a ListView and our user requires we create a checkbox to Select/Deselect all. I have the event handler for when the check box state changes and call the routine to set everything to Selected.
private void SelectAllEventHandler(object sender, EventArgs e)
{
ChangeState(RadCapListView, SelectAllRadcap.Checked);
}
private void ChangeState(SWF.ListView control, bool state)
{
if (control.CheckBoxes)
{
control.Items.OfType<SWF.ListViewItem>().ToList()
.ForEach(item => item.Checked = state);
}
else
{
control.Items.OfType<SWF.ListViewItem>().ToList()
.ForEach(item => item.Selected = state);
}
control.Refresh();
}
Going into debug mode all items are marked as selected.
Also at the control level SelectedItems is properly updated.
The issue is that visually the control just will not highlight the selected items like we have our WPF forms doing. As you can see in the code I also tried to refresh the control hoping that would show items selected, but no joy.
Has anyone solved this problem in getting selected items to display properly?
Thank!
Instead of using control.Refresh(), try control.Focus().
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;
}
}
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;
}
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;
}
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;
}
}