简体   繁体   English

WPF处理转换器错误并查看模型验证

[英]WPF Handling converter errors and view model validation

I have an MVVM application with INotifyDataErrorInfo all set up and functioning. 我有一个带有INotifyDataErrorInfo的MVVM应用程序,它们均已设置并运行。 Currently I am able to validate all my properties and gate the user from progressing until all errors are handled. 目前,我能够验证我的所有属性,并控制用户前进,直到处理完所有错误为止。 Everything was sunshine and roses until recently when we realized that some of our controls (textbox and label combined for displaying digits and unit type ie 100mm) weren't displaying the red error decorator when the values were deleted/backspaced. 直到最近,当我们意识到我们的某些控件(用于显示数字和单位类型(即100mm)的文本框和标签的组合)在删除/退格时都没有显示红色错误修饰符时,一切都是阳光和玫瑰。

The control has a converter on it that takes in a double and formats it into a string for display in the textbox. 该控件上有一个转换器,该转换器接受一个double并将其格式化为字符串以显示在文本框中。 We found a problem in the convertback method where it was sending Binding.DoNothing if the convertback failed to convert the string back into a double. 如果convertback无法将字符串转换回double值,则在convertback方法发送Binding.DoNothing的问题中,我们发现了一个问题。 We changed that to DependencyProperty.UnsetValue and figured everything would work fine, but that uncovered a bigger problem. 我们将其更改为DependencyProperty.UnsetValue并认为一切正常,但是发现了一个更大的问题。

The problem we are having now is that the viewmodel is not being notified that the converter is failing and I'm not sure how to do that. 我们现在遇到的问题是,没有通知viewmodel转换器发生故障,我不确定该怎么做。 When a user deletes the textbox content the converter gets called with an empty string. 当用户删除文本框内容时,将使用空字符串调用转换器。 That empty string won't parse out into a double. 空字符串不会解析为双精度型。

I feel like I need a way to inform the VM that the convertback failed so it can flag the property as hasError. 我觉得我需要一种方法来通知VM转换失败,以便可以将属性标记为hasError。 I don't see any way to do this though. 我看不到有什么办法。 Here is the code for the convertback method. 这是convertback方法的代码。

        public object[] ConvertBack(object value, Type[] targetTypes, object           parameter, CultureInfo culture)
    {
        var strVal = value as string;
        double dblVal = 0;
        bool isSuccess = double.TryParse(strVal, out dblVal);

        if (_systemUnits == SystemUnits.Metric && isSuccess)
        {
            return new[] { dblVal, Binding.DoNothing };
        }
        else if (_systemUnits != SystemUnits.Metric && isSuccess)
        {
            return new[] { Binding.DoNothing, dblVal };
        }
        else
        {
            return new[] { Binding.DoNothing, Binding.DoNothing };
        }
    }
}

I'm not sure what other code is pertinent, but I'm willing to post more if you think it would help. 我不确定其他什么相关的代码,但是如果您认为有帮助,我愿意发布更多代码。 Thanks for your time! 谢谢你的时间!

I feel like I need a way to inform the VM 我觉得我需要一种通知虚拟机的方法

Provide a static off of the app class which has the latest instance of the VM in question and in the converter access that static and call a method/property off of it directly to inform of status. 在应用程序类中提供静态对象,该类具有所讨论的VM的最新实例,并在转换器中访问该静态对象,并直接从其调用方法/属性以通知状态。

-Or- -要么-

I would recommend that you create computed properties which do the same work as the converters to provide estimates of the failures you mention. 我建议您创建计算属性,这些属性的作用与转换器相同,以提供您提到的故障的估计值。

Then you can just access the computed properties and do the process you need to accomplish. 然后,您可以仅访问计算的属性并完成您需要完成的过程。


Here is an example property IsOverLimit which could be bound if needed and checked for statuses via its INotifyPropertyChange etc... 这是一个示例属性IsOverLimit ,可以根据需要绑定该属性,并通过其INotifyPropertyChange等检查状态。

public bool IsOverLimit { get { return ThreadCreationLimit > 120 } }

 public uint ThreadCreationLimit
    {
        get { return _ThreadCreationLimit; }
        set
        {
            _ThreadCreationLimit = value;
            OnPropertyChanged("ThreadCreationLimit");
            OnPropertyChanged("IsOverLimit");
        }
    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM