Creating a child window without an MVVM frame

I am writing a small application learning MVVM in WPF.

As long as I continue to use one window, everything is pretty simple. Now I want to open a new window with a specific ViewModel.

I have a main ViewModel that contains a command that should open a new Window / ViewModel along with the parameter.

my main window

To do this using the MVVM method, I created a navigation service, which I would call so:

    public MainWindowViewModel()
    {
        DetailsCommand = new DelegateCommand(Details);
    }

    public void Details()
    {
        SessionsViewModel sessions = new SessionsViewModel();
        _NavigationService.CreateWindow(sessions);
    }

I noticed that you can "bind" views and ViewModels in XAML, for example:

<Application x:Class="TimeTracker.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:TimeTracker"
             xmlns:vm="clr-namespace:TimeTracker.ViewModels"
             xmlns:vw="clr-namespace:TimeTracker.Views"
             StartupUri="Views/MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <DataTemplate DataType="{x:Type vm:MainWindowViewModel}">
                <vw:MainWindow />
            </DataTemplate>
            <DataTemplate DataType="{x:Type vm:SessionsViewModel}">
                <vw:Sessions />
            </DataTemplate>
        </ResourceDictionary>
    </Application.Resources>
</Application>

<Window x:Class="TimeTracker.Views.Sessions"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TimeTracker.Views"
        xmlns:vm="clr-namespace:TimeTracker.ViewModels"
        mc:Ignorable="d"
        Title="Sessions" Height="300" Width="300">
    <Window.DataContext>
        <vm:SessionsViewModel/>
    </Window.DataContext>
    <Grid>
        <TextBlock Text="Hallo" />
    </Grid>
</Window>

The problem I am facing is that I do not know how I can use this ResourceDictionary in my NavigationService so that I can create a new window using only the ViewModel.

class NavigationService
{
    public void CreateWindow(IViewModel viewModel)
    {
        //How do I create a new Window using the ResourceDictionary?
    }
}
+4
1

, ContentControl ContentPresenter , ViewModel. , . Application.Resources , WPF DataTemplate.

, Window DataTemplate. (, Grid, StackPanel ..).

:

<blah:MyChildWindow>
     <ContentControl Content={Binding DataContext}/>
</blah:MyChildWindow>

And in Application.Resources:

<DataTemplate DataType={x:Type blah:MyViewModel}>
     <blah:MyChildWindow/>
</DataTemplate>

BTW - DataTemplates , , - .

+1

Source: https://habr.com/ru/post/1623326/


All Articles