不使用MVVM框架创建子窗口

4

我正在学习WPF中的MVVM,同时编写一个小应用程序。

只要我继续使用一个窗口,一切都很容易。 现在,我想打开一个具有特定ViewModel的新窗口。

我有一个主ViewModel,其中包含一个应该打开新窗口/ViewModel的命令,以及一个参数。

我的主窗口

为了以MVVM方式实现此操作,我创建了一个NavigationService,并希望像这样调用它:

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

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

我注意到在XAML中可以像这样“绑定”视图和视图模型:

<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>

我遇到的问题是我不知道如何在NavigationService中使用这个ResourceDictionary,以便我只能使用其ViewModel创建一个新窗口。
class NavigationService
{
    public void CreateWindow(IViewModel viewModel)
    {
        //How do I create a new Window using the ResourceDictionary?
    }
}

2
https://dev59.com/A18e5IYBdhLWcg3wVJTH#25846192 - Jeremy
1个回答

1

确保你的新窗口中有ContentControl或ContentPresenter,这样ViewModel才能被展示。接下来,确保资源字典在作用域内。将其放置在Application.Resources中将使其成为全局的,并保证WPF可以找到DataTemplate。

此外,不要在DataTemplate中使用Window类作为你的视图。使用你的子窗口面板(例如Grid、StackPanel等)。

我这样做:

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

And in Application.Resources:

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

顺便提一下 - 按照您尝试的方式使用DataTemplates是一个非常好的模式。


谢谢你的回答。我已经更新了我的XAML绑定以显示它是全局的。你能解释一下为什么你的MyChildWindow中只有一个ContentControl吗?我想在View中放置一些内容,比如Grid或List。请查看我的更新后的View。 - Benjamin Diele
使用您正在使用的模式,视图内容由DataTemplate本身(或其中引用的UserControl /自定义控件)提供。我建议使用ContentControl,因为它将应用WPF可以找到的任何DataTemplate到其内容ViewModel。如果您有一系列项目而不是单个项目,则可以使用ItemsControl,例如ListBox-它也将为列表中的每个项目找到任何DataTemplate。 - hoodaticus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接