简体   繁体   English

属性转换器的自定义错误消息

[英]Custom errormessage for property converter

Is it possible to use a ressource to set the errormessage on a property convert error? 是否可以使用资源在属性转换错误上设置错误消息?

[Range(0, 10, ErrorMessageResourceName = "Error", ErrorMessageResourceType = typeof(Properties.Resources))]
[DataMember]
public int MyProperty
{
  get; set;
}

The problem occourres if I try to set MyProperty to "aaa". 如果我尝试将MyProperty设置为“ aaa”,则会出现问题。 I get an error saying "Cannot convert 'aaa'". 我收到一条错误消息:“无法转换'aaa'”。 It is that message that I wish to customize. 我希望自定义该消息。

A really good way to do this would be within a Try/Catch statement. 一个真正好的方法是在Try / Catch语句中。 The following is in VB.net, as I'm not yet perfectly well versed in VB. 以下是VB.net中的内容,因为我对VB尚不十分了解。

Try 'Your code for setting the property. 尝试使用“您的代码设置属性”。 Catch MessageBox.Show("Your error message text here", "Your Error Title Here", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try 捕获MessageBox.Show(“此处为您的错误消息文本”,“此处为您的错误标题”,MessageBoxButtons.OK,MessageBoxIcon.Error)

Basically, you are going to create a try/catch statement, with the code for setting the property under "Try". 基本上,您将创建一个try / catch语句,其中包含用于在“ Try”下设置属性的代码。 Then, under "Catch", you're going to show a message box. 然后,在“捕获”下,您将显示一个消息框。

I found a solution for my problem. 我找到了解决问题的方法。 I have created an extention for the controls I use in my XAML code (typically a TextBox). 我为XAML代码(通常是TextBox)中使用的控件创建了扩展。 In the extention I handle the format exception and find the appropriate resource. 在扩展方面,我处理格式异常并找到适当的资源。

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

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