Example T144439
Visible to All Users

Pass data between View Models through the ISupportParameter interface

This example passes data between View Models through the ISupportParameter interface (using the DevExpress MVVM Framework).

Files to Look At

Documentation

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

View/ChildView1.xaml
XAML
<UserControl x:Class="Example.View.ChildView1" 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:ChildViewModelBase/> </UserControl.DataContext> <Grid> <TextBlock Text="This View has a ViewModel derived from the ViewModelBase class" TextWrapping="Wrap"/> </Grid> </UserControl>
View/ChildView2.xaml
XAML
<UserControl x:Class="Example.View.ChildView2" 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:ChildPOCOViewModel}"> <Grid> <TextBlock Text="This View has a POCO ViewModel" TextWrapping="Wrap"/> </Grid> </UserControl>
View/MainView.xaml
XAML
<UserControl x:Class="Example.View.MainView" 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:View="clr-namespace:Example.View" xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="500" d:DesignWidth="600" DataContext="{dxmvvm:ViewModelSource ViewModel:MainViewModel}"> <dxmvvm:Interaction.Behaviors> <dx:DXMessageBoxService/> </dxmvvm:Interaction.Behaviors> <Grid x:Name="LayoutRoot" Background="White"> <StackPanel Orientation="Vertical"> <StackPanel Orientation="Horizontal"> <Button Content="Change Parameter1" Command="{Binding ChangeParameter1Command}"/> <Button Content="Change Parameter2" Command="{Binding ChangeParameter2Command}"/> </StackPanel> <StackPanel Orientation="Horizontal"> <Border BorderThickness="1" BorderBrush="Black" Margin="10" Width="300" Height="100"> <View:ChildView1 dxmvvm:ViewModelExtensions.ParentViewModel="{Binding DataContext, ElementName=LayoutRoot}" dxmvvm:ViewModelExtensions.Parameter="{Binding DataContext.Parameter1, ElementName=LayoutRoot}"/> </Border> </StackPanel> <StackPanel Orientation="Horizontal"> <Border BorderThickness="1" BorderBrush="Black" Margin="10" Width="300" Height="100"> <View:ChildView2 dxmvvm:ViewModelExtensions.ParentViewModel="{Binding DataContext, ElementName=LayoutRoot}" dxmvvm:ViewModelExtensions.Parameter="{Binding DataContext.Parameter2, ElementName=LayoutRoot}"/> </Border> </StackPanel> </StackPanel> </Grid> </UserControl>
ViewModel/ChildPOCOViewModel.cs(vb)
C#
using DevExpress.Mvvm; using DevExpress.Mvvm.DataAnnotations; namespace Example.ViewModel { public class ChildPOCOViewModel : ISupportParameter { [ServiceProperty(SearchMode=ServiceSearchMode.PreferParents)] protected virtual IMessageBoxService MessageBoxService { get { return null; } } public virtual object Parameter { get; set; } protected virtual void OnParameterChanged() { if(Parameter is string) MessageBoxService.Show("ChildPOCOViewModel: Parameter = " + (string)Parameter); } } }
ViewModel/ChildViewModelBase.cs(vb)
C#
using DevExpress.Mvvm; namespace Example.ViewModel { public class ChildViewModelBase : ViewModelBase { IMessageBoxService MessageBoxService { get { return GetService<IMessageBoxService>(ServiceSearchMode.PreferParents); } } protected override void OnParameterChanged(object parameter) { base.OnParameterChanged(parameter); if(parameter is string) MessageBoxService.Show("ChildViewModelBase: Parameter = " + (string)parameter); } } }
ViewModel/MainViewModel.cs(vb)
C#
namespace Example.ViewModel { public class MainViewModel { public virtual string Parameter1 { get; set; } public virtual string Parameter2 { get; set; } public void ChangeParameter1() { counter1++; Parameter1 = "Parameter #" + counter1.ToString(); } public void ChangeParameter2() { counter2++; Parameter2 = "Parameter #" + counter2.ToString(); } int counter1 = 0; int counter2 = 0; } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.