2

I'm having a rather strange problem while working with a ListBox on a WPF program. I've defined my ListBox SelectionMode as Single, but it appear that even if visually I cannot select multiples items, it happens in the code. As can be seen here : SelectionModeSingleWithMultipleSelected

When trying to do a workaround, I tried to use the function UnselectAll, it rather strangly still leave an item selected : UnselectAllStillSelected

And as a last gift, once multiple items have been selected, if I try to click on my unselected visualy, selected in the code, item my application crash with a System.ArgumentException (I suppose it tries to select my item, that's already selected in fact, so it crashes because of adding a perfect duplicate ?) ApplicationCrashOnListBoxClick

I looked into ListBox is selecting many items even in SelectionMode="Single" that had kindof the same problem, but in my case I cannot visually select multiples items, and my items are completly distinct so it doesn't really help.

I don't have any custom behavior on ListBox.Click that could mess with something ...

my ListBox.ItemsSource is bound to a list of Items. I will decrement the value of a property of the selected Item when calling useItem on it, but it is still the same item (I don't recreate it/remove it then add it again)

Where is my problem coming from ? How can I fix it ?

Community
  • 1
  • 1
Belterius
  • 738
  • 1
  • 10
  • 20

2 Answers2

5

So, finally found the origin of my problem.

Having a custom Item, I had to redefine my Equals function. I then had to redefine my GetHashCode function too, which I based on several of my properties, including the field I am decrementing ...
So when I would change the field, I would at the same time change my HashCode and I couldn't access my item anymore.

Changing my GetHashCode function to base it on an immutable field solved my problem.

Belterius
  • 738
  • 1
  • 10
  • 20
1

If your SelectionMode is Single, you should not bind to SelectedItems as per documentation. Just use SelectedItem.

aksu
  • 1,748
  • 1
  • 11
  • 15
  • I don't. My binding looks as follow : `listItemSave = new BindingList(hero.backPack.getItems);` `listBoxBackPackItem.ItemsSource = this.listItemSave;` I never use `SelectedItems`, it is only visible on the `Watch`, I only use `SelectedItem` in the code. edit : just seen that I missunderstood a previous comment, thinking it was asking if I had my listItems (my ItemsSource) bound, my mistake. I'm not sure I can edit my previous comment to correct my mistake. – Belterius Aug 15 '16 at 13:07
  • Ok, good. How do you notice the issue (except when debugging)? Is there more than one item shown as selected in the UI? Is the `SelectedItem` the wrong item? What does you `Food` class look like? – aksu Aug 15 '16 at 14:15
  • The SelectedItem is the first item in SelectedItems, it means if I try to select another Item and act on it, it will still try the action on the first item. And then when I try to select my first item again (so he's not selected visually but is present in my SelectedItems) my application will then crash as per my third screenshot. – Belterius Aug 15 '16 at 16:01
  • My food class contains a string (name) and an int (nbCharges). In my function I will decrement the nbCharges. In case you'd need it the code can be found in it's entirety here https://github.com/Belterius/LDVELH/tree/WPFBranch/LDVELH_WPF The related files would be mainwindow.xaml.cs, Item.cs and EventHandler.cs – Belterius Aug 15 '16 at 16:02
  • 1
    I can't pinpoint the exact problem, but I noticed that you don't use `Binding` at least for the `ItemsSource` property and pretty much everything is set in the code behind. I would suggest refactoring a bit: start using MVVM-pattern, it will make your life with WPF easier. – aksu Aug 16 '16 at 09:00
  • I thought that setting my ItemsSource = List was equivalent to binding, I'll try to change things and see if it gets better. As for refactoring, I agree, I discovered too late into my project the recommended implementation, so I was planning on having everything working and then trying to refactor, but I still have some trouble with the concept so I'm learning on other smaller sample project before refactoring this one. – Belterius Aug 16 '16 at 12:40