MVVM-如何将视图模型绑定到视图 [英] MVVM - How do I Bind the View Model to the View

查看:102
本文介绍了MVVM-如何将视图模型绑定到视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

,并先谢谢您的指导.我是MVVM的新手,而且我一直在通过文章学习.我想我已经走得很远了,但是似乎有一件事情让我无法逃脱.如何(不使用后面代码中的代码)自动绑定到所需的视图?据我了解,如果正确完成,这就是模式的工作方式.我可以使用xaml主窗口中的代码完成所有操作,而且我什至可以正确地创建资源字典(因为我可以在直接窗口中访问它).我只是无法进行自动化的下一步.'这可能是我的设计,因为我不熟悉这种模式,因此完全可以接受我做错了一切的可能性.这就是我所拥有的...

and thanks in advance for the guidance. I'm new to MVVM, and I've been learning via articles. I think I've gotten pretty far, but there's one thing that seems to escape me. How do I (not using code in the code behind) automatically bind to the view I want? As I understand it, if done correctly, this is how the pattern should work. I can make this all happen using code behind in the main window xaml, and I have even created a resource dictionary correctly (as I can access it in the immediate window.) I just can't get to that next step of 'automation.' It could be my design, as I new to this pattern I'm totally amenable to the possibility that I've done this all wrong. Here's what I have...

我有一个主窗口.这是一个3行的网格.第一行是菜单.最下面一行是状态栏.中间是一个堆栈面板,根据菜单选择动态加载内容.

I have a main window. It's a grid with 3 rows. The top row is a menu. The bottom row is a status bar. The middle is a stack panel where the content is dynamically loaded based on the menu selection.

我有2个用于填充此堆栈面板的视图.一个只不过是样式化的文本框(帮助和关于).另一个本身就是复合视图:搜索面板,结果网格和详细信息面板均已加载到停靠管理器框架中.

I have 2 views that I am using to fill this stack panel. One has nothing more than a styled textbox in it (Help & About.) The other is itself a composite view: a search panel, a results grid and a detail panel all loaded into a dock manager frame.

在后面的主窗口代码中,当用户单击菜单选项时,我清除堆栈面板的子级,实例化视图模型,实例化将视图模型传递到其中的视图,然后将新视图添加到子级中的堆栈.效果很好,但我认为这与模式不符.

In the main window code behind, when the user clicks a menu option, I clear the children of the stack panel, instantiate the view model, instantiate the view passing the view model into it and then add the new view into the children of the stack. This works fine, but I don't think it's consistent with the pattern.

正如我提到的,我有资源字典,但是我不知道如何将其与堆栈面板关联.我以为我必须使用绑定,但是我无法弄清楚如何绑定到资源字典和/或如何告诉它在命令上更改视图.

As I mentioned I have the resource dictionary, but I don't know how to associate it with the stack panel. I assume that I have to use binding, but I can't figure out how to bind to the resource dictionary and/or how to tell it to change views on command.

我读过一些文章,这些文章将所有可用的视图模型添加到视图模型的只读列表中,该列表本质上充当主寡妇和所需的实际视图模型之间的过渡.这似乎还可以,但是我不明白为什么需要资源字典.而且,这些示例是向导实现,在这种情况下,这似乎是一种不错的方法,但是我无法想象对具有100个视图模型的应用程序执行此操作.

I have read articles that have added all of the available view models to a read only list in a view model that essentially acts as the go between the main widow and the actual view models needed. This seems OK, but I don't understand why the resource dictionary is needed then. Moreover, these examples were wizard implementations and in that scenario this seems like a good approach, but I can't imagine doing that for an application with say 100 view models.

再次,对我的无知感到抱歉,但我希望有人能指出我正确的方向.正如我说的,我已经阅读了大量文章(乔什·史密斯,戴夫·希尔等),但我仍然没有建立联系,所以我希望获得一些具体的指导.(我确实有WPF即将发布,但是我希望在此之前能有所进步.)

Again, sorry for my ignorance, but I was hoping that someone could point me in the right direction. As I said, I've read a ton of articles (Josh Smith, Dave Hill, etc.) and I still haven't made the connection, so I was hoping for some concrete guidance. (I do have WPF Unleashed on the way, but I was hoping to make some progress before then.)

任何人都可以帮忙吗?

推荐答案

有很多方法可以绑定视图模型.

There are ways on how to bind your view models.

1.在XAML中创建静态资源.

<Window x:Class="WpfApplication2.MainWindow"
        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:WpfApplication2"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
  <Window.Resources>
    <local:MainViewModel x:Key="MainVM" />
  </Window.Resources>
  <Grid DataContext="{StaticResource MainVM}">

  </Grid>
</Window>

2.绑定到View的构造函数中

我知道您提到过不使用代码,但这也是一种选择.只要您在后面的代码中不编写任何逻辑,就可以了.

I know you mentioned not using code behind but this is also an option. As long as you don't write any logic in the code behind then you're good.

public MainWindow()
{
  InitializeComponent();
  this.DataContext = new MainViewModel(); 
}

3.使用ViewModelLocator绑定

您可能想要创建一个视图模型定位器类,该类负责为您的视图提供所需的视图模型.

You might want to create a view model locator class that is in charge of giving your view the viewmodel that it needs.

这是一个视图模型定位器类的简单示例.viewmodel定位器类公开了一些viewmodel属性.然后,稍后将这些属性绑定到视图的数据上下文.

Here's a simple example of a viewmodel locator class. The viewmodel locator class exposes some viewmodel properties. Then later on we will bind these properties to the data context of the views.

 public class ViewModelLocator
  {
    public ViewModelLocator()
    {
      this.MainVM = new MainViewModel();
      this.AnotherVM = new AnotherViewModel();
    }
    public MainViewModel MainVM { get; set; }
    public AnotherViewModel AnotherVM { get; set; }
  }

然后,您可以在App.xaml中创建viewmodel定位器的静态资源,以使其可用于应用程序中的所有视图.

Then you can create a static resource of the viewmodel locator in the App.xaml to make it available to all the views within the app.

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

然后,您可以将视图的数据上下文绑定到viewmodel定位器的属性.

Then you can bind your view's data context to the viewmodel locator's property.

该示例告诉我们,如果将ViewModel定位器(即MainViewModel实例)绑定到Window的数据上下文,则您正在将MainVM属性绑定.

The example tells us that you are binding the MainVM property if the viewmodel locator, which is a MainViewModel instance, to the Window's data context.

<Window x:Class="WpfApplication2.MainWindow"
        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:WpfApplication2"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525"
        DataContext="{Binding MainVM, Source={StaticResource Locator}}">

这篇关于MVVM-如何将视图模型绑定到视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆