WPF ListBox Selection Problem when changing an item?

After searching a bit more I found the solution. Adding an IsSynchronizedWithCurrentItem to the ListBox solved the problem.

Up vote 2 down vote favorite share g+ share fb share tw.

When changing the selected item in a ListBox, I'm getting a weird error where the changed item appears selected but I cannot deselect it or reselect it. Is there a way to fix this? Here's a sample app that demonstrates the problem.

Public partial class Window1 : Window { public Window1() { InitializeComponent(); this. DataContext = new WindowViewModel(); lst. SelectedIndex = 0; } private void Button_Click(object sender, RoutedEventArgs e) { ((WindowViewModel)this.

DataContext). Items0 = "New Item"; } } public class WindowViewModel { public WindowViewModel() { Items = new ObservableCollection(); Items. Add("Item1"); Items.

Add("Item2"); Items. Add("Item3"); } public ObservableCollection Items { get; set; } } c# .net wpf xaml listbox link|improve this question asked May 13 '09 at 3:28Cameron MacFarland15k64670 75% accept rate.

That isn't a good answer though and I personally would not be happy with it, so I had a go and found an alternative. This situation only occurs IF the item you are changing is currently selected and you are binding directly to the source. Use a CollectViewSource between the ListBox and your view model.

Can't explain what is going on in your example, may be a bug with WPF, but this is a better/cleaner solution. I suspect IsSynchronizedWithCurrentItem="True" is creating a view source for you behind-the-scenes and that is why it works. HTH.

The problem is with the SelectedItems, not the ItemsSource. – Bryan Anderson May 13 '09 at 14:55 So why don't you like using IsSynchronizedWithCurrentItem? – Cameron MacFarland May 13 '09 at 18:30.

I also had this problem where I couldnt deselect any items. It seems that the listbox doesn't understand when you have multiple items in the list which are exactly the same. (and thus it selects them all, but doesn't deselect them) this also happens when you change the value of the selected item when your binding.

You have to first deselect the index (ex: listBox. SelectedIndex = -1;) and then change the value. I hope this helps anyone with the same problem as me.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions