简体   繁体   中英

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". I get an error saying "Cannot convert 'aaa'". It is that message that I wish to customize.

A really good way to do this would be within a Try/Catch statement. The following is in VB.net, as I'm not yet perfectly well versed in 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

Basically, you are going to create a try/catch statement, with the code for setting the property under "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). In the extention I handle the format exception and find the appropriate resource.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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