Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.
#How to use MessageBoxService in ViewModels derived from the ViewModelBase class
This example demonstrates how to use the DXMessageBoxService in View Models derived from the ViewModelBase class. The View Models are related to each other by the parent-child relationship with the ISupportParentViewModel interface.
<UserControlx:Class="Example.View.ChildView"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:ViewModel="clr-namespace:Example.ViewModel"xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"mc:Ignorable="d"d:DesignHeight="300"d:DesignWidth="300"><UserControl.DataContext><ViewModel:ChildViewModel/></UserControl.DataContext><Grid><ButtonContent="ChildView: Show Message"Command="{Binding ShowMessageCommand}"/></Grid></UserControl>
usingDevExpress.Mvvm;
usingSystem.Windows.Input;
namespaceExample.ViewModel {
publicclassMainViewModel : ViewModelBase {
public ICommand ShowMessageCommand { get; privateset; }
IMessageBoxService MessageBoxService { get { return GetService<IMessageBoxService>(); } }
publicMainViewModel() {
ShowMessageCommand = new DelegateCommand(ShowMessage);
}
voidShowMessage() {
MessageBoxService.Show("This is MainView!");
}
}
}
ImportsDevExpress.MvvmImportsSystem.Windows.InputNamespace Example.ViewModel
PublicClass ChildViewModel
Inherits ViewModelBase
Private privateShowMessageCommand As ICommand
PublicProperty ShowMessageCommand() As ICommand
GetReturn privateShowMessageCommand
EndGetPrivateSet(ByVal value As ICommand)
privateShowMessageCommand = value
EndSetEndPropertyPublicSubNew()
ShowMessageCommand = New DelegateCommand(AddressOf ShowMessage)
EndSubPrivateReadOnlyProperty MessageBoxService() As IMessageBoxService
GetReturn GetService(Of IMessageBoxService)(ServiceSearchMode.PreferParents)
EndGetEndPropertyPrivateSub ShowMessage()
MessageBoxService.Show("This is ChildView")
EndSubEndClassEndNamespace
ImportsDevExpress.MvvmImportsSystem.Windows.InputNamespace Example.ViewModel
PublicClass MainViewModel
Inherits ViewModelBase
Private privateShowMessageCommand As ICommand
PublicProperty ShowMessageCommand() As ICommand
GetReturn privateShowMessageCommand
EndGetPrivateSet(ByVal value As ICommand)
privateShowMessageCommand = value
EndSetEndPropertyPrivateReadOnlyProperty MessageBoxService() As IMessageBoxService
GetReturn GetService(Of IMessageBoxService)()
EndGetEndPropertyPublicSubNew()
ShowMessageCommand = New DelegateCommand(AddressOf ShowMessage)
EndSubPrivateSub ShowMessage()
MessageBoxService.Show("This is MainView!")
EndSubEndClassEndNamespace
This example demonstrates how to use the DXMessageBoxService in POCO View Models. The View Models are related to each other by the parent-child relationship with the ISupportParentViewModel interface.
<UserControlx:Class="Example.View.ChildView"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:ViewModel="clr-namespace:Example.ViewModel"xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"mc:Ignorable="d"d:DesignHeight="300"d:DesignWidth="300"DataContext="{dxmvvm:ViewModelSource ViewModel:ChildViewModel}"><Grid><ButtonContent="ChildView: Show Message"Command="{Binding ShowMessageCommand}"/></Grid></UserControl>
ImportsDevExpress.MvvmImportsSystem.Windows.InputNamespace Example.ViewModel
PublicClass MainViewModel
ProtectedOverridableReadOnlyProperty MessageBoxService() As IMessageBoxService
GetReturnNothingEndGetEndPropertyPublicSub ShowMessage()
MessageBoxService.Show("This is MainView!")
EndSubEndClassEndNamespace
ImportsDevExpress.MvvmImportsDevExpress.Mvvm.DataAnnotationsNamespace Example.ViewModel
PublicClass ChildViewModel
<ServiceProperty(SearchMode:=ServiceSearchMode.PreferParents)> _
ProtectedOverridableReadOnlyProperty MessageBoxService() As IMessageBoxService
GetReturnNothingEndGetEndPropertyPublicSub ShowMessage()
MessageBoxService.Show("This is ChildView")
EndSubEndClassEndNamespace
#How to use MessageBoxService in a custom View Model
This example demonstrates how to use the DXMessageBoxService in a custom View Model (not derived from the ViewModelBase class and not a POCO View Model).
Refer to the Services in custom ViewModels topic for more information on how to use the Service mechanism in a custom View Model.
Custom View Models in this example are related to each other with the parent-child relationship. This is achieved by supporting the ISupportParentViewModel interface in the View Models.
ImportsDevExpress.MvvmImportsSystem.Windows.InputNamespace Example.ViewModel
PublicClass ChildViewModel
Implements ISupportServices, ISupportParentViewModel
Private serviceContainer_Renamed As IServiceContainer = NothingProtectedReadOnlyProperty ServiceContainer() As IServiceContainer
GetIf serviceContainer_Renamed IsNothingThen
serviceContainer_Renamed = New ServiceContainer(Me)
EndIfReturn serviceContainer_Renamed
EndGetEndPropertyPrivateReadOnlyProperty ISupportServices_ServiceContainer() As IServiceContainer Implements ISupportServices.ServiceContainer
GetReturn ServiceContainer
EndGetEndPropertyPrivateProperty ISupportParentViewModel_ParentViewModel() AsObjectImplements ISupportParentViewModel.ParentViewModel
PrivateReadOnlyProperty MessageBoxService() As IMessageBoxService
GetReturn ServiceContainer.GetService(Of IMessageBoxService)(ServiceSearchMode.PreferParents)
EndGetEndPropertyPrivate privateShowMessageCommand As ICommand
PublicProperty ShowMessageCommand() As ICommand
GetReturn privateShowMessageCommand
EndGetPrivateSet(ByVal value As ICommand)
privateShowMessageCommand = value
EndSetEndPropertyPublicSubNew()
ShowMessageCommand = New DelegateCommand(AddressOf ShowMessage)
EndSubPrivateSub ShowMessage()
MessageBoxService.Show("This is ChildView")
EndSubEndClassEndNamespace
ImportsDevExpress.MvvmImportsSystem.Windows.InputNamespace Example.ViewModel
PublicClass MainViewModel
Implements ISupportServices
Private serviceContainer_Renamed As IServiceContainer = NothingProtectedReadOnlyProperty ServiceContainer() As IServiceContainer
GetIf serviceContainer_Renamed IsNothingThen
serviceContainer_Renamed = New ServiceContainer(Me)
EndIfReturn serviceContainer_Renamed
EndGetEndPropertyPrivateReadOnlyProperty ISupportServices_ServiceContainer() As IServiceContainer Implements ISupportServices.ServiceContainer
GetReturn ServiceContainer
EndGetEndPropertyPrivateReadOnlyProperty MessageBoxService() As IMessageBoxService
GetReturn ServiceContainer.GetService(Of IMessageBoxService)()
EndGetEndPropertyPrivate privateShowMessageCommand As ICommand
PublicProperty ShowMessageCommand() As ICommand
GetReturn privateShowMessageCommand
EndGetPrivateSet(ByVal value As ICommand)
privateShowMessageCommand = value
EndSetEndPropertyPublicSubNew()
ShowMessageCommand = New DelegateCommand(AddressOf ShowMessage)
EndSubPrivateSub ShowMessage()
MessageBoxService.Show("This is MainView!")
EndSubEndClassEndNamespace
<UserControlx:Class="Example.View.ChildView"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:ViewModel="clr-namespace:Example.ViewModel"xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"mc:Ignorable="d"d:DesignHeight="300"d:DesignWidth="300"><UserControl.DataContext><ViewModel:ChildViewModel/></UserControl.DataContext><Grid><ButtonContent="ChildView: Show Message"Command="{Binding ShowMessageCommand}"/></Grid></UserControl>