【问题标题】:Creating a child Window without an MVVM framework创建没有 MVVM 框架的子窗口
【发布时间】:2016-01-07 19:35:34
【问题描述】:

我正在编写一个小型应用程序,同时在 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?
    }
}

【问题讨论】:

标签: c# wpf xaml mvvm


【解决方案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>

顺便说一句 - 以您尝试的方式使用 DataTemplates 是一种极好的模式。

【讨论】:

  • 感谢您的回答。我更新了我的 XAML 绑定以表明它是全局的。你能解释一下为什么你的MyChildWindow 中只有一个 ContentControl 吗?我正在尝试将一些内容放入视图中,例如网格或列表。请查看我的更新视图。
  • 使用您使用的模式,视图内容由 DataTemplate 本身(或您在其中引用的 UserControl / 自定义控件)提供。我建议使用 ContentControl,因为它将 DataTemplate WPF 可以找到的任何内容应用于其内容 ViewModel。如果您有一个项目列表而不是单个项目,则可以使用像 ListBox 这样的 ItemsControl - 它也将应用它可以为列表中的每个项目找到的任何 DataTemplate。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-01
  • 1970-01-01
  • 2021-04-17
  • 2011-07-18
  • 1970-01-01
相关资源
最近更新 更多