ListBox and selectedIndexChanged event after the user hit the back button - windows-phone-7

In my windows phone 7 app, I have the following code to handle the OnSelectedIndexChange of a ListBox.
private void wordList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
WordList selectedList = (WordList)e.AddedItems[0];
NavigationService.Navigate(new Uri("/Views/Game.xaml?ListName=" + selectedList.Name, UriKind.RelativeOrAbsolute));
}
The above code work fine, However if the user click on the hardware back button from the Game page, and click on the same listbox item, the above code is not called. I assume this is because the selected item is the same therefore the SelectionChanged event is not being called.
How do I make it so that if the user select the same item I can still send them to the Game page?
I looked at the Tap Event but I couldn't figure out a way to get the selected Item from the tab event.

When using SelectionChanged to navigate, surround your navigation logic with a check to see if the SelectedIndex = -1. After you navigate, set the index to -1, so that the event will not fire twice.
private void wordList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var lb = sender as ListBox;
if (lb != null)
{
if (lb.SelectedIndex == -1) return;
WordList selectedList = (WordList)e.AddedItems[0];
NavigationService.Navigate(new Uri("/Views/Game.xaml?ListName=" + selectedList.Name, UriKind.RelativeOrAbsolute));
lb.SelectedIndex = -1;
}
}

This way you can get the selected Item from the Tap event.
private void wordList_Tap(object sender, GestureEventArgs e)
{
var selectedElement = e.OriginalSource as FrameworkElement;
if (selectedElement != null)
{
var selectedData = selectedElement.DataContext as WordList;
if (selectedData != null)
{
NavigationService.Navigate(new Uri("/Views/Game.xaml?ListName=" + selectedData.Name, UriKind.RelativeOrAbsolute));
}
}
}

I had such issue within a UserControl. Check the sender, and return if it is not the ListBox control which is triggering the event:
protected void cbEvents_SelectedIndexChanged(object sender, EventArgs e)
{
if (sender is DropDownList)
RebindGrid();
}

Related

Xamarin - How to know what has been clicked in collection view?

I have a CollectionView with an image and a button in it. I use following code to see if somebody pressed anywhere within the cell:
private void CollectionView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (((CollectionView)sender).SelectedItem != null)
{
var item = (picdata)e.CurrentSelection.FirstOrDefault();
((CollectionView)sender).SelectedItem = null;
if (allowfullscreen == "1" || allowfullscreen == "true")
{
Navigation.PushAsync(new Picture());
}
}
}
But how can I know if he clicked the button inside the cell? I was trying to do it via the Click event, but then I do not know which one of all the buttons has been clicked.
you can get the item from the BindingContext of the sender
var item = (picdata)(Button)sender.BindingContext;

Selection changed event also called Lostfocus event?

NET C# ,
In my windows phone 7.5 application , I want to make visible the application bar if any item has selected .. So I am making it visible in selected change event. But what is happening in my code is when ever selection change it also triggers LostFocus event and in that event I am making selected index = 0.
Now the resultant of the code is when ever I select any item , application bar gets visible then automatically invisible ( because of lost focus event).
Following is the piece of code .
private void ShopingListItemDetails_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ShopingListItemDetails.SelectedIndex != -1)
{
ApplicationBar.IsVisible = true;
int selind = ShopingListItemDetails.SelectedIndex;
}
}
private void ShopingListItemDetails_LostFocus(object sender, RoutedEventArgs e)
{
ApplicationBar.IsVisible = false;
ShopingListItemDetails.SelectedIndex = -1;
}
I am just at start with .NET C#(XAML) so assuming that selection change event is also triggering LostFocus event.
Please help me what is the real problem behind.Thanks
Zauk
You can use the following hack. Initialize a variable, say selectChanged to False initially in the xaml.cs. In SelectionChanged function change it to True. Now, in the LostFocus function do processing only if the selectChanged variable is false, and if it is true set it back to False
Boolean selectChanged=false;
private void ShopingListItemDetails_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ShopingListItemDetails.SelectedIndex != -1)
{
ApplicationBar.IsVisible = true;
int selind = ShopingListItemDetails.SelectedIndex;
selectChanged=true;
}
}
private void ShopingListItemDetails_LostFocus(object sender, RoutedEventArgs e)
{
if(!selectChanged)
{
ApplicationBar.IsVisible = false;
ShopingListItemDetails.SelectedIndex = -1;
}
selectChanged=false;
}
I think this should solve your problem.

Adding events dynamically

I have n picture boxes. They should perform the following events dynamically:
private void pictureBoxMouseHover(object sender, EventArgs e)
{
if (sender is PictureBox)
{
((PictureBox)sender).BorderStyle = BorderStyle.FixedSingle;
}
}
private void pictureBoxMouseLeave(object sender, EventArgs e)
{
if (sender is PictureBox)
{
((PictureBox)sender).BorderStyle = BorderStyle.None;
}
}
private void MainMaster_Load(object sender, EventArgs e)
{
foreach (var control in Controls)
{
if (sender is PictureBox)
{
PictureBox pb=new PictureBox();
pb.Name = sender.ToString();
pb.MouseHover += new System.EventHandler(this.pictureBoxMouseHover);
pb.MouseLeave += new System.EventHandler(this.pictureBoxMouseHover);
}
}
}
I couldn't find what wrong with this; please help me.
dbaseman is right, you used wrong variable when iterating through controls.
But if you want to add this behavior to all picture boxes, then better solution is to create custom picture box, and simply place it on your form:
public class MyPictureBox : PictureBox
{
protected override void OnMouseHover(EventArgs e)
{
BorderStyle = BorderStyle.FixedSingle;
base.OnMouseHover(e);
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
BorderStyle = BorderStyle.None;
}
}
Create this class, compile app and drag these custom picture boxes from toolbox to your form. They all will display border when mouse hover over picture box.
I think the mistake is here:
foreach (var control in Controls)
{
if (sender is PictureBox)
Sender in this case will be the window. I think you intended control.
foreach (var control in Controls)
{
if (control is PictureBox)

windows phone 7 ListBox event confusion

The app is like a small dictionary. I have a listbox and a textbox. The list box is already filled with words and when there is any entry in the textbox the listbox is refilled again with words starting with the letters in the textbox. I have a listbox SelectionChanged event implemented when the user clicks on a word its meaning appears. The problem is when user selects a word from the list and then types something in the textbox, listBox SelectionChanged event is called i dont want this to happen because at this point of time my listbox's selected item is empty.I would like to have a event that is fired only when user selects something from the listbox. It should not be fired when the content of the listbox changes. Thank you
You can use
1.if (lstWords.SelectedItem != null)
2.lstWords.SelectedIndex = -1;
for e.g. following is the source code for text changed event and list selection change event
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
try
{
if (textBox1.Text.ToString().Equals(""))
{
XmlDictionaryRepository test = new XmlDictionaryRepository();
lstWords.ItemsSource = test.GetWordList(categorySelected,xmlFileName);
}
else
{
XmlDictionaryRepository test = new XmlDictionaryRepository();
lstWords.ItemsSource = test.GetMatchWordList(categorySelected, textBox1.Text.ToString(),xmlFileName);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), (((PhoneApplicationFrame)Application.Current.RootVisual).Content).ToString(), MessageBoxButton.OK);
}
}
private void lstWords_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
if (lstWords.SelectedItem != null)
{
string wordSelected = ((Glossy_Test.Dictionary)(lstWords.SelectedItem)).Word;
if (lstWords.SelectedItem != null)
{
NavigationService.Navigate(new Uri(string.Format("/DescribeWord.xaml?param1={0}&param2={1}", wordSelected, categorySelected), UriKind.Relative));
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), (((PhoneApplicationFrame)Application.Current.RootVisual).Content).ToString(), MessageBoxButton.OK);
}
finally
{
// lstWords.SelectedIndex = -1;
}
}

Selecting First Item in the ListBox on CollectionViewSource.Filter

My ListBox is Binded with CollectionView Source. When I am Changing the Filter it is Automatically selecting the First Item in the Listox.
App.ViewModel.TasksViewSource.Filter += new System.Windows.Data.FilterEventHandler(Tasks_Filter);
void Tasks_Filter(object sender, System.Windows.Data.FilterEventArgs e)
{
if (e.Item == null)
return;
Task task = e.Item as Task;
e.Accepted = task.Id.Equals(TaskId);
}
private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (TasksListBox.SelectedIndex == -1)
return;
Task selectedTask = App.ViewModel.AllTasks[TasksListBox.SelectedIndex];
TasksListBox.SelectedIndex = -1;
NavigationService.Navigate(new Uri("/Views/TaskDetailsPage.xaml?taskId=" + selectedTask.Id, UriKind.Relative));
}
Please Help Me.
Set your ListBox IsSynchronizedWithCurrentItem="False".
What really do you want ?
I you do not want the first item being selected when changing filter, you have first to create a private Task object (and/or a SelectedTask property implementing INotifyPropertyChanged).
On the SelectionChanged event of your listbox, set the SelectedTask with the current selected Task.
Then, after having applied your filter, bind the SelectedItem property to SelectedTask.

Resources