1

Is there any way to update a ListBox control with Dictionary binded to it without resetting its DataSource property on every dictionary change? The reason I don't like this solution is that it forces a ListBox control to jump to the first item as mentioned in other questions like this.

Here is a minimal example that reproduces this behavior -- https://bitbucket.org/ntrophimov/updating_issue (about 20 lines of code to read in the MainForm.cs file)

Is there any other solution for this problem?

Is there any dictionary implementation in which I can manipulate items (add and remove them) and these changes will be immediately represented in a ListBox control without refreshing its whole content?

Community
  • 1
  • 1
FrozenHeart
  • 19,844
  • 33
  • 126
  • 242
  • So you want to build something like `Observable` `Dictionary` ? – Alex F Sep 09 '15 at 11:12
  • @Jasper Maybe, I just want to fix this updating behavior – FrozenHeart Sep 09 '15 at 11:21
  • Hmmm.. If you worried for visual selection you could save it as `Selected={Binding SelectedItem}` and just restore it back after you update the `ItemsSource` setter/getter – Alex F Sep 09 '15 at 11:23
  • @Jasper I'm using WinForms, not WPF (look at the tags) – FrozenHeart Sep 09 '15 at 11:33
  • But does my solution looks good (if we rephrase it for WinForms) ? or you looking for something else ? – Alex F Sep 09 '15 at 12:03
  • @Jasper I didn't understand you at all. Could you rephrase it pls? – FrozenHeart Sep 09 '15 at 13:21
  • The idea is: 1. You show your `ListBox` with items 2. User click on some item 3. Applications save the 'selected' item in local variable 4. User/App initiate `ListBox` update process 5. Update process done -> application restore (re-assign) the 'selected' item (if it still valid for current items of `ListBox`) – Alex F Sep 09 '15 at 14:26
  • @Jasper Then a user will see a flickering because of reassigning the selected item I think – FrozenHeart Sep 09 '15 at 14:29
  • I don't remember exactly but I guess in WinForms there was something like `control.StartEditing()` and `control.EndEditiing()` or something like this... Some functions that could temporary suspend GUI thread from refreshing your control until you release it – Alex F Sep 09 '15 at 14:31
  • @Jasper Ok, but what if a user didn't select any item? He just scrolled a `ListBox` control some time and then `DataSource` reassigning scrolled it back to the first item in the list – FrozenHeart Sep 09 '15 at 14:34
  • Hmm... This is default behaviour of list controls. There's not build-in restore-position mechanism in Windows controls. You could mimic this by hooking on 'selected' item but without hooking.... I'm not sure that WinForms `ListBox` has a scrool position property - and if so you must be 100% sure that this is almost the same items because it will look strange to scrool to the same position for different items in `ListBox`... Are you sure that you have those requirements ? – Alex F Sep 09 '15 at 14:40

1 Answers1

0

The idea is:

  1. You show your ListBox with items
  2. User click on some item
  3. Applications save the 'selected' item in local variable
  4. User/App initiate ListBox update process
  5. Update process done -> application restore (re-assign) the 'selected' item (if it still valid for current items of ListBox)
Alex F
  • 3,180
  • 2
  • 28
  • 40