Skip to main content

WindowService

  • 3 minutes to read

The WindowService allows you to show your view as a window, and control the displayed window from the ViewModel.

The WindowService service implements the IWindowService interface.

Get Started

Attach the Service

To show a window from a ViewModel, use the WindowService. For this assign the WindowService service to your View. You can do this in the following ways:

  • attach the WindowService service to a View with Quick Actions

  • declare the WindowService service manually as follows:

    <UserControl ...
                 xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm">
        <Grid>
            <dxmvvm:Interaction.Behaviors>
                <dxmvvm:WindowService/>
            </dxmvvm:Interaction.Behaviors>
            ...
        </Grid>
    </UserControl>
    

Access the Service

Refer to the following topics for more information how to access a service in ViewModel.

The code sample below shows how to access the service from a ViewModel that is a DevExpress.Mvvm.ViewModelBase descendant:

public class MainViewModel : ViewModelBase {
    protected IWindowService WindowService { get { return this.GetService<IWindowService>(); } }
    ...
}

Display a Window

To display a window with a View, use one of the IWindowService‘s Show extension methods depending on your MVVM architecture:

Name

Description

Show(object viewModel)

Use this method when the View is defined though the service’s ViewTemplate or ViewTemplateSelector property. The ViewModel is passed via the parameter.

Show(string documentType, object viewModel)

Use this method when both the View and ViewModel are passed via the method’s parameters.

Show(string documentType, object parameter, object parentViewModel)

Use this method when the View, parameter, and parent ViewModel are passed via the method’s parameters. The ViewModel is explicitly specified at the View’s level.

protected IWindowService WindowService { get { return this.GetService<IWindowService>(); } }

public void ShowChildWindow() {
    if(ChildWindowViewModel == null) 
        ChildWindowViewModel = new ChildViewModel() { Caption = "Hello, World!" };
    WindowService.Show("ChildView", ChildWindowViewModel);
}

Refer to the following help topic for more information on how to pass a View to the Show method: ViewLocator.

Manage the Displayed Window

The IWindowService interface provides a set of members useful to manipulate (for example, close or activate it) the displayed window in code.

The code sample below demonstrates how to close the displayed window from code.

protected IWindowService WindowService { get { return this.GetService<IWindowService>(); } }

void CloseChildWindow() {
    ChildWindowViewModel = null;
    WindowService.Close();
}

Adjust the Service Window

Use the following WindowService properties that allow you to configure your service window.

Name

Description

WindowStartupLocation

Specifies the position of the service window when it is first shown.

AllowSetWindowOwner

Specifies if the service’s window owner is assigned automatically.

WindowType

Specifies the service’s window type.

WindowStyle

Specifies the service’s window style.

Title

Specifies the service’s window title.

WindowShowMode

Specifies if the service’s window is modal or modeless.

<UserControl ...
             xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
             xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm">
    <Grid>
        <dxmvvm:Interaction.Behaviors>
            <dxmvvm:WindowService AllowSetWindowOwner="True"
                                  WindowStartupLocation="CenterOwner">
                <dxmvvm:WindowService.WindowStyle>
                    <Style TargetType="dx:ThemedWindow">
                        <Setter Property="Width" Value="500" />
                        <Setter Property="Height" Value="300" />
                    </Style>
                </dxmvvm:WindowService.WindowStyle>
            </dxmvvm:WindowService>
        </dxmvvm:Interaction.Behaviors>
        ...
    </Grid>
</UserControl>

WindowService Sample Project

View Example