c# - Handling errors in WPF MVVM -


i using wpf mvvm im 1 of project. have datagrid bind list of objects.

<datagrid  itemssource="{binding path=listofvalues}" margin="5,38"  

in view model class have property listofvalues

public observablecollection<classa> listofvalues         {             { return listofvalues; }             set             {                 listofvalues= value;                 raisepropertychangedevent("listofvalues");             }         } 

in classa have 3 properties.

public string name { get; set; } public long no { get; set; }         public decimal amount { get; set; } 

in grid user can enter value amount filed.i want validate whether user enters valid decimal value filed.

suggest me place can catch execption. try handle @ close of window. if user enters invalid value not saved in data context of view. tried validate in setter of classa not hit setter of value.

perhaps attack problem different angle... how stopping users entering non-numerical characters textbox in first place?

you can using previewtextinput , previewkeydown events... attach handlers textbox in question , add code them:

public void textcompositioneventhandler(object sender, textcompositioneventargs e) {     // if last pressed key not number or full stop, ignore     return e.handled = !e.text.all(c => char.isnumber(c) || c == '.'); }  public void previewkeydowneventhandler(object sender, keyeventargs e) {     // if last pressed key space, ignore     return e.handled = e.key == key.space; } 

if want take bit of time re-usability, can put attached property... it's great able add functionality property:

<textbox text="{binding price}" attached:textboxproperties.isdecimalonly="true" /> 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -