Say I want to display some validation error to the user. In the MVVM pattern, I could have a label that is bound to some property on my viewmodel. But what if I wanted to show a message box while strictly adhering to the MVVM pattern. What would my viewmodel bind to, and how would it trigger a message box to be created/displayed?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
Have an interface
IMessageBoxService
as:Create a
WPFMessageBoxService
class:In your
ViewModel
accept IMessageBoxService as a constructor parameter and injectWPFMessageBoxService
using DI/IoC.In the ViewModel, use
IMessageBoxService.ShowMessage
to show the MessageBox.Customize
IMessageBoxService
interface to your needs, and pick up a better name.You could bind your messagebox control's visibility to the validation.
You will need a Bool To Visibility converter for this.
See here for using the built in converter: Binding a Button's visibility to a bool value in ViewModel