PowerSwitcher | Power plan switcher for Windows
kandi X-RAY | PowerSwitcher Summary
kandi X-RAY | PowerSwitcher Summary
Power plan switcher for Windows 10. Heavily inspired by EarTrumpet.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of PowerSwitcher
PowerSwitcher Key Features
PowerSwitcher Examples and Code Snippets
Community Discussions
Trending Discussions on PowerSwitcher
QUESTION
I have a ListBox
that has its ItemsSource
bound to a custom class that (properly) implements an INotifyCollectionChanged
and a SelectedItem
bound to a field in a ViewModel.
The problem is that when I remove a currently SelectedItem
from the ItemsSource
collection it immediately changes the selection to an neighboring item. I would very much prefer if it just removed selection.
The reason why it's such a problem for me is following. The ItemsSource
class contains elements from some other collection that either satisfy some (during runtime constant) Predicate or are Active
. Being Active
is "synchronized" with being SelectedItem
(there're reasons for that). So it's very much possible for an item be allowed in the ListBox
only if it's selected which means it might be supposed to vanish when user selects some other one.
My function (deep in "model") that gets called when SelectedItem
gets changed:
ANSWER
Answered 2017-Mar-19 at 11:49The key to your problems is that you set IsSynchronizedWithCurrentItem="True"
on your ListBox
. What it does is it keeps the ListBox.SelectedItem
and ListBox.Items.CurrentItem
in sync. Also, ListBox.Items.CurrentItem
is synchronized with the ICollectionView.CurrentItem
property of the default collection view for the source collection (this view is returned by CollectionViewSource.GetDefaultView(Schemas)
in your case). Now when you remove an item from the Schemas
collection which also happens to be the CurrentItem
of the corresponding collection view, the view by default updates its CurrentItem
to the next item (or the previous one if the removed item was the last one, or to null
if the removed item was the only item in the collection).
The second part of the problem is that when the ListBox.SelectedItem
is changed causing an update to your view-model property, your RaisePropertyChangedEvent(nameof(ActiveSchema))
is processed after the update process is finished, in particular after the control is returned from the ActiveSchema
setter. You can observe that the getter is not hit immediately, but only after the setter is done. What's important, the CurrentItem
of the Schemas
view is also not updated immediately to reflect the newly selected item. On the other hand, when you set IsActive = false
on the previously selected item, it causes immediate "removal" of this item from the Schemas
collection, which in turn causes an update of the CurrentItem
of the collection view, and the chain immediately continues to update the ListBox.SelectedItem
. You can observe that at this point the ActiveSchema
setter will be hit again. So your ActiveSchema
will be changed again (to the item next to the previously selected one) even before you've finished processing the previous change (to the item selected by the user).
There are several ways to address this issue:
#1
Set the IsSynchronizedWithCurrentItem="False"
on your ListBox
(or leave it untouched). This will make your problem go away with no effort. If however for some reason it is required, use any of the other solutions.
#2
Prevent reentrant attempts to set the ActiveSchema
by using a guard flag:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install PowerSwitcher
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page