简体   繁体   中英

Silverlight 4 TextBox not validating

We have a SL 4 application utilizing an MVVM architecture and pretty clean data binding between objects and UI controls. We have created a custom ValidationAttribute that we use to decorate our domain classes and to provide field level validation based on a data store. If we utilize these custom validation attributes and manually validate our classes using the Validator class like this:

        bool _isValid = Validator.TryValidateObject(this.DataContext, new ValidationContext(this.DataContext,null,null), results,true);

then the custom validation attributes are utilized and everything works fine.

Our issue is that, when simply entering data into a textbox bound to a field decorated with one of these validation attributes we do not see immediate validation feedback (via the default red border and tooltip styling of the tool box).

A sample XAML snippet of a control not validating upon lost focus is:

        <TextBox Text="{Binding AssetID,Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnDataErrors=true}"></TextBox>

Any suggestions on what we are overlooking or what we can do to force SL to validate text boxes upon lost focus and not wait until a page level save is done (at which point we just validate with the Validator class)?

You have to validate the value in the property setter of the property you're bound to.

You can see what I mean in this post. http://msdn.microsoft.com/en-us/magazine/ee335695.aspx

Can you post the property you're binding to, specifically the setter code?

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