我如何绑定的DataContext到一个通用的视图模型在XAML? [英] How Can I bind DataContext to a Generic ViewModel in XAML?

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

问题描述

假设我们有这样一个通用的视图模型:

Suppose we have a generic View model like this:

public class MyViewModel<T> : INotifyPropertyChanged where T : Class1
{
    private T _objectModel;
    public MyViewModel(T object)
    {
        _objectModel= object;
    }
    public event PropertyChangedEventHandler PropertyChanged;
}

当我想这个视图模型绑定的DataContext我的用户控件 XAML ,我不能! XAML编辑器没有找到我的视图模型类。 ?我应该怎样指在XAML泛型类型

When I want to bind this View Model to DataContext of my UserControl in XAML, I can not! XAML editor does not find My View Model class. How should I refer to a generic type in XAML?

<UserControl.DataContext>
    <s:MyViewModel<T>/> // How should I write this here????
</UserControl.DataContext> 

在上面的代码取值是一个别名我的工作区,如果我将我的通用视图模型来具体的类它正常工作。

In the above code s is an alias for my workspace, and If I convert my generic View Model to a concrete class it works normally.

推荐答案

在使用XAML工作,你不能实例化与XAML代码中的泛型参数视图模型。

When working with XAML, you cannot instantiate a view model with a generic parameter in XAML code.

要解决这个问题,你需要使用继承的,这里的一个例子:

To work around this, you need to make use of inheritance, here's an example:

public abstract class ViewModel<T>



用法:

Usage:

public class MovieViewModel : ViewModel<Movie>

...

public class GenreViewModel : ViewModel<Genre>



创建一个新的类为每个模型的看起来的是一个有点笨,然而,这根本不是真的。通过使每个视图模型中包含一个模型的假设,你已经差不多为自己设定为以下所有视图模型这种模式,因为你的基础视图模型的实施此约束。

Creating a new class for each model seems to be a bit stupid, however this simply isn't true. By making the assumption that each view model contains one model, you've pretty much set yourself up for following this pattern in all view models, as your base view model enforces this constraint.

我个人使用使用视图模型<的格局; T> 基类,其中 T 是模型。

I personally use the pattern of using a ViewModel<T> base class, where T is the model.

这当然是一个好主意,让您的基本视图模型分离的逻辑。每个模型视图模型实际上是在实现一个很好的模式。

It's certainly a good idea to keep the logic separated from your base view model. A view model for each model is in fact a very good pattern to implement.

有另一种方法可以实现什么你后,这仅仅是从视图模型底座拆卸通用,考虑例如:

There is another way you can achieve what you're after, this is simply removing the generic from the view model base, consider the example:

public class ViewModel
{
    public object Model { get; protected set; }
}

现在,如果你填写模式与比方说,一个电影,那么XAML就会看到它作为一个电影,而不是对象。现在,这是非常漂亮的,只要你身边的XAML去,但是,当你开始使用这个模型中工作的 C#,然后你将有各种各样的问题,你必须投对象为任何类型所使用。所以我不建议这在所有。

Now, if you populate Model with let's say a Movie, then XAML will see it as a Movie, and not an object. Now this is pretty nifty as far as your XAML side goes, however, when you start working with this model in C#, then you're going to have all sorts of problems as you'll have to cast the object to whatever type you are using. So I wouldn't recommend this at all.

解决这个获得的另一种方法是将的DataContext 中的代码隐藏,如果你要做到这一点,那么,好了,只有上帝才能拯救现在你。围绕MVVM设计模式的主要思想是从逻辑的分离和业务层(视图模型),只要您的视图开始实例化视图模型那么漂亮的分离丢失。

Another method of getting around this would be to set the DataContext in code-behind, and if you're going to do that, then, well, only God can save you now. The main ideas around the MVVM design pattern is the separation of View logic and the Business layer (View Models), as soon as your View starts instantiating view models then that nice separation is lost.

现在这么说,并没有什么东西做这个阻止你,我以前很多次说。 MVVM是一种设计模式,不规律,如果要设置的DataContext 中的代码隐藏,然后不够公平,但它是非常重要的你是知道的含义。

Now saying that, there's nothing stopping you from doing this, I've said this many times before. MVVM is a design pattern, not the law, if you want to set the DataContext in code-behind, then fair enough, however it's important that you are aware of the implications.

这篇关于我如何绑定的DataContext到一个通用的视图模型在XAML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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