在 MVVM 中设置可见性的最佳方法 [英] Best Approach of setting the Visibility in MVVM

查看:47
本文介绍了在 MVVM 中设置可见性的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 View 中,我有三个对象,其中一个在任何给定时间都是可见的.在我的 Model 中,我有一个枚举来表示三种状态.

In my View I have three objects, of which one is Visible at any given time. In my Model I have an enumeration to represent the three states.

我应该如何实现我的 ViewModel?

How should I implement my ViewModel?

a) 为每个对象的可见性创建一个布尔值,并将每个对象绑定到此(使用 bool->visibility 转换器).

a) Create a boolean for the visibility of each object, and bind each object to this (with a bool->visibility converter).

b) 绑定到枚举,每个对象都有一个唯一的转换器.

b) Bind to the enum, with a unique converter for each object.

c) 绑定到枚举,使用一个带参数的转换器.

c) Bind to the enum, with a single converter that takes a parameter.

d) 使用带有布尔值关键帧的可视状态管理器,并通过附加属性从 VM 驱动状态.

d) Use a visual state manager with boolean key frames, and drive the state from VM with an attached property.

e) 从后面的代码绑定到 VM 枚举,并通过代码设置可见性.

e) Bind to the VM enum from code behind, and set visibility thru code.

f) ?

我真的很希望答案是 f)(即明显的选择让我逃过一劫),因为我对 a) 到 e 并没有真正感到高兴).

I am seriously hoping the answer is f) (ie the obvious choice that escapes me), because I am not really overjoyed with a) through e).

欢迎提出想法并表示赞赏.

Thoughts welcome and appreciated.

推荐答案

MVVM 中的最佳方法并不一定意味着简单.我喜欢以下方法:

The Best approach in MVVM does not necessarily mean easy. I like the following approaches:

a) 为每个对象的可见性创建一个布尔值,并将每个对象绑定到此(使用 bool->visibility 转换器).

a) Create a boolean for the visibility of each object, and bind each object to this (with a bool->visibility converter).

此方法是为Control设置Visibility的最直观、最经典的方法.

This method is the most intuitive and classically for setting Visibility for Control.

b) 绑定到枚举,每个对象都有一个唯一的转换器.

b) Bind to the enum, with a unique converter for each object.

c) 绑定到枚举,使用一个带参数的转换器.

c) Bind to the enum, with a single converter that takes a parameter.

在转换器的情况下,Enum 最好不要放在 ModelView.因为问题解决到了View的一边,这是非常合乎逻辑的,在这里存储数据结构.原则上,这并不重要.

In the case of the Converter, Enum is the best keep not in the Model and in the side of View. Because the problem solves over to the side of View, which is quite logical and here to store the data structure. In principle, it is not critical.

示例:

public sealed class InvertableBooleanToVisibilityConverter : IValueConverter
{
    enum Parameters
    {
        Normal, 
        Inverted
    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var boolValue = (bool)value;
        var direction = (Parameters)Enum.Parse(typeof(Parameters), (string)parameter);

        if (direction == Parameters.Inverted)
            return !boolValue ? Visibility.Visible : Visibility.Collapsed;

        return boolValue ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return DependencyProperty.UnsetValue;
    }
}

关于其他方法的一些评论:

A couple of comments about other approaches:

d) 使用带有布尔值关键帧的可视状态管理器,并通过附加属性从 VM 驱动状态.

d) Use a visual state manager with boolean key frames, and drive the state from VM with an attached property.

对于这些情况,看起来很复杂,所以没有看到其中的意义.但是,如果设置 Visibility 的条件不难,则可以使用 VisualStateManager.

For these situations, it looks complicated, so do not see the point in it. But, if the conditions of setting Visibility are not difficult, you can use VisualStateManager.

e) 从后面的代码绑定到 VM 枚举,并通过代码设置可见性.

e) Bind to the VM enum from code behind, and set visibility thru code.

在这种情况下,当您可以使用 MVVM 的典型工具(绑定、转换器等)解决问题时,代码隐藏是不合理的.我认为,在这种情况下,如果选择Visibility的元素不涉及业务逻辑,例如可能通过按下来设置Visibility,则不会违反MVVM的原则CheckBoxToggleButton

Code-behind in this case is not justified when you can solve the problem using typical tools of MVVM (Binding, Converters, etc). I think, in this case it would not be a violation of the principle of MVVM, if choosing the element to the Visibility is not involved business logic, such as may come setting Visibility by pressing of CheckBox, ToggleButton, etc.

这篇关于在 MVVM 中设置可见性的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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