.net 如何使用 DataContext 属性在 XAML 中的窗口上设置 ViewModel?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4590446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:05:24  来源:igfitidea点击:

How do I set a ViewModel on a window in XAML using DataContext property?

.netwpfxamlmvvm

提问by Nicholas

The question pretty much says it all.

这个问题几乎说明了一切。

I have a window, and have tried to set the DataContext using the full namespace to the ViewModel, but I seem to be doing something wrong.

我有一个窗口,并尝试使用完整的命名空间将 DataContext 设置为 ViewModel,但我似乎做错了什么。

<Window x:Class="BuildAssistantUI.BuildAssistantWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    DataContext="BuildAssistantUI.ViewModels.MainViewModel">

回答by Josh

Try this instead.

试试这个。

<Window x:Class="BuildAssistantUI.BuildAssistantWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:VM="clr-namespace:BuildAssistantUI.ViewModels">
    <Window.DataContext>
        <VM:MainViewModel />
    </Window.DataContext>
</Window>

回答by Merlyn Morgan-Graham

In addition to the solution that other people provided (which are good, and correct), there is a way to specify the ViewModel in XAML, yet still separate the specific ViewModel from the View. Separating them is useful for when you want to write isolated test cases.

除了其他人提供的解决方案(很好且正确)之外,还有一种方法可以在 XAML 中指定 ViewModel,但仍将特定的 ViewModel 与 View 分开。当您想编写隔离的测试用例时,将它们分开非常有用。

In App.xaml:

在 App.xaml 中:

<Application
    x:Class="BuildAssistantUI.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:BuildAssistantUI.ViewModels"
    StartupUri="MainWindow.xaml"
    >
    <Application.Resources>
        <local:MainViewModel x:Key="MainViewModel" />
    </Application.Resources>
</Application>

In MainWindow.xaml:

在 MainWindow.xaml 中:

<Window x:Class="BuildAssistantUI.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    DataContext="{StaticResource MainViewModel}"
    />

回答by Jobi Joy

You need to instantiate the MainViewModel and set it as datacontext. In your statement it just consider it as string value.

您需要实例化 MainViewModel 并将其设置为数据上下文。在您的声明中,它只是将其视为字符串值。

     <Window x:Class="BuildAssistantUI.BuildAssistantWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:BuildAssistantUI.ViewModels">
      <Window.DataContext>
        <local:MainViewModel/>
      </Window.DataContext>

回答by Geert van Horrik

You might want to try Catel. It allows you to define a DataWindow class (instead of Window), and that class automatically creates the view model for you. This way, you can use the declaration of the ViewModel as you did in your original post, and the view model will still be created and set as DataContext.

您可能想尝试Catel。它允许您定义一个 DataWindow 类(而不是 Window),该类会自动为您创建视图模型。这样,您可以像在原始帖子中一样使用 ViewModel 的声明,并且视图模型仍将被创建并设置为 DataContext。

See this articlefor an example.

有关示例,请参阅本文