简体   繁体   English

在使用MVVM时,我应该在哪里放置WPF特定代码?

[英]Where should I put WPF specific code when using MVVM?

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. 我刚刚开始使用MVVM,但到目前为止我看到的所有示例都是将View控件绑定到简单的非WPF特定数据类型,如字符串和整数。 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? 目前,我将数字转换为ViewModel中的画笔以仅保留View XAML,但是这是正确的吗?

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. 我不喜欢将WPF特定代码放在ViewModel中,但同样我不喜欢在我的View面板上放置代码隐藏的想法。

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? 目前,我将数字转换为ViewModel中的画笔以仅保留View XAML,但是这是正确的吗?

No, not really. 不,不是真的。

Ideally, you should keep WPF dependencies out of your ViewModel. 理想情况下,您应该将WPF依赖项保留在ViewModel之外。 This helps allow your application to be more testable, but also easily translatable to Silverlight or other technologies in the future. 这有助于您的应用程序更易于测试,但也可以在将来轻松转换为Silverlight或其他技术。

WPF provides a mechanism for this exact scenario, however: IValueConverter . 但是,WPF为这种确切的场景提供了一种机制: IValueConverter It is very easy to make a ValueConverter that does the translation from an integer, string, or any other type, into a brush. 使用整数,字符串或任何其他类型转换为画笔的ValueConverter非常容易。 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. 从长远来看,这是一个更好的设计......“画笔”和其他WPF概念实际上是视图的一部分 - 它们与你的逻辑无关。 Your ViewModel should think in terms of state , and your View should translate that state to a specific way to represent the state. 您的ViewModel应该根据状态进行思考,并且您的View应该将该状态转换为表示状态的特定方式。

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 . 它应该暴露一些原语(即:bool属性),例如IsInErrorState ,而不是暴露一个画笔的ViewModel。 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. 视图应该决定如何表示 - 无论是通过红色画笔,大警告等等......转换器允许这种情况以纯XAML方式发生。


In your case, the ValueConverter is easy. 在您的情况下,ValueConverter很容易。 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: 因为你要从数字 - >刷子(虽然我建议使用自定义的Enum而不是int),你可以做类似的事情:

[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();
    }
}

Try custom ValueConverter. 尝试自定义ValueConverter。

What's the purpose of keeping your view XAML only? 仅保留您的XAML视图的目的是什么? Keeping ViewModel clean makes sense because of testability and SoC. 由于可测试性和SoC,保持ViewModel清洁是有意义的。 But no codebehind? 但没有代码隐藏?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WPF MVVM,我应该在哪里放置视图特定的属性? - WPF MVVM, where should I put view specific properties? WPF \\ MVVM-我应该在哪里放置视图相关属性? - Wpf \ MVVM - Where should I put view related properties? 使用MVVM模式时,应在哪里放置与View紧密连接但又庞大的方法? - Where should I put methods that are strongly connected to the View, but are massive in size, when using the MVVM pattern? 代码中的哪个位置以及如何在使用MVVM时告诉文本框显示临时加载消息? - Where in code and how should I be telling a text box to display a temporary loading message when using MVVM? 寻找将某些代码放在WPF MVVM应用程序中的指导 - Looking for guidance for where to put some code in a WPF MVVM application WPF MVVM,我需要把我的转换器? - WPF MVVM where i need to put my converters? 我应该将我的数据放在哪里在MVVM中放置程序时不会被丢弃 - where should I put my data that will not be discarded while programs on In MVVM Prism MVVM,我应该将IRegionManager放在ViewModel中还是其他地方? - Prism MVVM, Should I put IRegionManager in ViewModel or some where else? 我应该把这些代码放在哪里随时执行? - Where should i put these code to be executed anytime? 我应该在哪里放置automapper代码? - Where should I put automapper code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM