检查两个DecimalUpDown控件之间的有效值-MVVM [英] Check for Valid Values between two DecimalUpDown Controls - MVVM

查看:277
本文介绍了检查两个DecimalUpDown控件之间的有效值-MVVM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的窗口中有两个DecimalUpDown控件.一个应显示TextBox的最大值,另一个应显示TextBox的最小值. 最小控件的值不能大于最大控件的值,反之亦然.

I have two DecimalUpDown Controls in my Window. One should show the maximum Value the other one minimum Value of the TextBox. The minimum control can not have greater values than the maximum one and vice versa.

请注意,红色值是错误的.

Notice that the red values are wrong one.

我该如何实现?我正在使用MVVM模式.

How can I implement this? I am using the MVVM pattern.

谢谢. 史蒂夫

推荐答案

您应该在视图模型中实现验证逻辑:

You should implement the validation logic in your view model:

public class MyViewModel : IDataErrorInfo
{
    private int _min;
    public int Min
    {
        get { return _min; }
        set { _min = value; }
    }

    private int _max;
    public int Max
    {
        get { return _max; }
        set { _max = value; }
    }

    public string Error { get { return null; } }

    public string this[string columnName]
    {
        get
        {
            switch(columnName)
            {
                case "Min":
                    if (_min > _max)
                        return "Min cannot be greater than Max";
                    break;
                case "Max":
                    if (_max < _min)
                        return "Max cannot be smaller than Min";
                    break;
            }

            return null;
        }
    }
}

XAML:

<xctk:IntegerUpDown Value="{Binding Min,ValidatesOnDataErrors=True}" />
<xctk:IntegerUpDown Value="{Binding Max,ValidatesOnDataErrors=True}" />

有关WPF中数据验证如何工作的更多信息,请参阅以下博客文章: https://blog.magnusmontin.net/2013/08/26/data-validation-in-wpf/.

Please refer to the following blog post for more information about how data validation in WPF works: https://blog.magnusmontin.net/2013/08/26/data-validation-in-wpf/.

您基本上在视图模型中实现了IDataErrorInfo或更新的INotifyDataErrorInfo.

You basiclly implement either the IDataErrorInfo or the newer INotifyDataErrorInfo in your view model.

这篇关于检查两个DecimalUpDown控件之间的有效值-MVVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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