문제

I have MainWindow with a statusbar and multiple user controls in it. Each user control has a viewmodel. How do I bind/update the statusbar text from multiple viewmodels?

도움이 되었습니까?

해결책

This is a simple one that I have used myself.

Implement a Messenger system and give it a Singleton instance, say... StatusBarMessenger

MainWindow View-Model:

  • Needs a string property, eg. Status.
  • Needs to register to the StatusBarMessenger in the Ctor
StatusBarMessenger.Instance.Register(this, new Action(s => Status = s));

All VM that requires to send text to the status bar may do the following:

StatusBarMessenger.Instance.Send("Hello world");

다른 팁

If you are using PRISM then you can use its EventAggregator to Publish (from your user controls) messages and in StatusBar VM Subscribe to those messages.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top