我应该在哪里把WPF特定的代码使用MVVM什么时候? [英] Where should I put WPF specific code when using MVVM?

查看:203
本文介绍了我应该在哪里把WPF特定的代码使用MVVM什么时候?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始加快速度在MVVM,但我到目前为止看到具有约束力查看的所有例子控件简单的非WPF特定数据类型,如字符串和整数。然而,在我们的应用程序我希望能够基于模型的一个数字来设置按钮的边框刷。



目前,我数转化为视图模型刷子只保留查看XAML,但对吗?



我不喜欢把WPF特定代码在视图模型,但同样,我不喜欢把代码隐藏我查看面板上的想法。



这是最好的方法是什么?



感谢


解决方案

目前,我翻译成数在视图模型刷子只保留查看XAML,但对吗?




没有,真的没有。



在理想情况下,你应该保持WPF依赖您的视图模型出来。这有助于让你的应用更容易测试,也容易翻译Silverlight的或未来的其他技术。



WPF为此提供了确切的情况的机制,但是:< A HREF =http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx相对=nofollow>的IValueConverter 。这是很容易做出ValueConverter,做平移从一个整数,字符串或任何其他类型的,成一刷。该数据绑定概述显示从颜色转换为Brush的例子。使用值转换器



这是从长远来看,一个更好的设计......刷等概念WPF真的是视图的一部分 - 他们AREN ŧ绑你的逻辑。您的视图模型应该考虑在状态服务条款,你的观应的状态转换为代表国家的特定方式。



你说你想要使用一个红色刷以显示一个错误。取而代之的是视图模型暴露一刷,它应该公开一些原始(即:一个布尔值属性),如 IsInErrorState 。该视图应该决定如何表述这 - 无论是通过一个红色的画笔,一个大的警告,等等...转换器允许这在纯粹的XAML的方式发生






在你的情况下,ValueConverter很容易。既然你从数去 - >刷(虽然我建议你使用自定义枚举,而不是一个int),你可以这样做:

  [ValueConversion(typeof运算(INT),typeof运算(的SolidColorBrush))] 
公共类IntToBrushConverter:的IValueConverter
{
公共对象转换(对象的值,类型TARGETTYPE,对象参数,System.Globalization.CultureInfo文化)
{
INT选项=(int)的值;
开关(选件)
{
默认:
返回Brushes.Black;
案例1:
返回Brushes.Red;
案例2:
返回Brushes.Green;
// ...
}

}

公共对象ConvertBack(对象的值,类型TARGETTYPE,对象参数,System.Globalization.CultureInfo文化)
{
//无需转换回在这种情况下,
抛出新NotImplementedException();
}
}


I'm just getting up to speed on MVVM, but all the examples I've seen so far are binding View controls to simple non-WPF specific data types such as strings and ints. However in our app I want to be able to set a button's border brush based on a number in the Model.

At the moment, I translate the number into a brush in the ViewModel to keep the View XAML only, but is that right?

I don't like putting WPF specific code in the ViewModel, but equally I don't like the idea of putting code-behind on my View panel.

Which is the best way?

Thanks

解决方案

At the moment, I translate the number into a brush in the ViewModel to keep the View XAML only, but is that right?

No, not really.

Ideally, you should keep WPF dependencies out of your ViewModel. This helps allow your application to be more testable, but also easily translatable to Silverlight or other technologies in the future.

WPF provides a mechanism for this exact scenario, however: IValueConverter. It is very easy to make a ValueConverter that does the translation from an integer, string, or any other type, into a brush. The Data Binding Overview shows an example of translating from a Color to a Brush using a Value Converter.

This is a much better design in the long run... "Brushes" and other WPF concepts are really part of the View - they aren't tied to your logic. Your ViewModel should think in terms of state, and your View should translate that state to a specific way to represent the state.

Say you want to use a "red" brush to display an error. Instead of the ViewModel exposing a brush, it should expose some primitive (ie: a bool property) such as IsInErrorState. The View should decide how to represent this - whether it be via a red brush, a big warning, etc... Converters allow this to happen in a purely XAML manner.


In your case, the ValueConverter is easy. Since you're going from a Number -> Brush (though I'd recommend using a custom Enum instead of an int), you can just do something like:

[ValueConversion(typeof(int), typeof(SolidColorBrush))]
public class IntToBrushConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        int option = (int)value;
        switch(option)
        {
            default:
                return Brushes.Black;
            case 1: 
                return Brushes.Red;
            case 2: 
                return Brushes.Green;
           // ...
        }

    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        // No need to convert back in this case
        throw new NotImplementedException();
    }
}

这篇关于我应该在哪里把WPF特定的代码使用MVVM什么时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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