Reputation: 21
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.
Upvotes: 2
Views: 829
Reputation: 11
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.
Upvotes: 0