How does WPF determine when to Invalidate using the CommandManager (CommandManager.InvalidateRequerySuggested)?
I've been using the RelayCommand a bit here and there, and it got me to wonder and want to know... when exactly does the CommandManager.InvalidateRequerySuggested()
get called by WPF?
My guess is every time any other routed event on UI get's triggered, which in the MVVM world primarily means when the the OnPropertyChanged event is raised, causing the value to change, causing some related routedevent to execute. But, that's just a guess, I'm wondering if anyone knows th开发者_运维知识库e exact inner workings here.
There are a few causes I know of:
When keyboard focus changes.
After a command is executed.
Controls can invoke this when as they deem necessary in some other cases (for example when you press a key while in TextBox).
Property change alone is not generally a reason for invalidation (I guess for performance reasons).
Sadly, I failed to find any documentation about this in MSDN. What I posted here is based on what I found in Pro WPF in C# 2010 (great book BTW).
Also, its not the CommandManager that gets invalidated. The CommandManager merely sends an event (to which controls using commands subscribe), so they can update their enabled/disabled status.
精彩评论