doing validation in WPF 4 for non-binding attributes - what approach? - c#

How should one handle validation in WPF 4 when there is no binding? Most of the validation doco I am reading seems to be for controls that have bindings.
For example, just a main windows with some text boxes that a user would fill out, and then a button someone would then click on. One could do it manually I guess but wouldn't' there be a WPF approach for this?
(any short code examples would be appreciated)

I think the most WPF solution would be to create a ViewModel you could bind to, then doing the validation there For example if its a change of password form with an additional "confirm password" field, this won't bind directly to a "user" model with just one password field. So instead create a viewModel with 2 fields "password1" and "password2", databind to these two properties and add representation-specific validation here.

Related

Accessing a button control of a page from another class Windows phone?

The problem I'm facing is that, I'm unable to access a control(button) which is in the MenuPanoramaPage.xaml from another page which is MyProfile.xaml.
I wanted to change the content within the button when I select the radio button which is in the MyProfile.xaml. How could I make this?
I tried using this within the button tag in xaml:
x:FieldModifier="public"
But it didn't work out. How can I work on this?
Thanks in advance.
I suggest you to use MVVM Pattern in which use can handle these kind of problems like handling data of one page from other page.
I give you a simple scenario :-
Suppose you want to change the content of a button that is on another page then what you have done basically is.
Some Points :-
1- > Define a string property in viewmodel of the page that contain button. and bind this property to the content property of the button.
2 -> So suppose you want to change it from other page then all you have to do is send a message from another page to change this property. And it will be automatically updated on button(you will get it after some example setup).
3 -> It is not related to some simple string contents. You handle whole data that will be displayed on that page and other View related properties too.(Basically MVVM is much more powerful than you can thought of).
4 -> Last point If you want to make a quality Windows Phone app then MVVM pattern is a good fit.
Links :-
How to implement MVVM in Windows Phone
Basic one from Channel9
What actually MVVM is ?
Explore those. You will not regret. Cheers :)
Sorry, but this is not possible. You only have direct access to the controls on the page you are on. In this case you can only change the controls in MyProfile.xaml.
But what you could do is sending an event or message (if you are using mvvm) to the MenuPanoramaPage.xaml / ViewModel.

Conditionally validate Dynamically generated Controls in Silverlight

I am having a form with different type of controls like Text Box, Drop downs, Check box, Radio buttons etc. All these controls are loaded dynamically from database at run time.
I want to perform validation on Text box on conditional basis. For example, If we have selected any value in drop down, then you must have to fill details in Text box. Otherwise text box details are not required.
I am open to use database to perform this task and I am using MVVM pattern in my project.
Any help on this is highly appreciated.
Thanks.
(I started this as a comment, but it ended up being too long).
In theory you have access to all these controls and their values in your ViewModel.
Without knowing the specifics of your program, it's difficult to suggest anything useful, but in essence you need to expose some more properties from your ViewModel (probably boolean) which will be calculated based on the values in your controls. Then you need to bind IsEnabled properties on your controls to these new properties.
Sounds simple, but I think you have some architectural problems which will make it difficult to implement what I suggested above. In order for this to work and automatically update your controls whenever other controls' content change, your ViewModel needs to implement INotifyPropertyChanged and raise PropertyChanged event every time you update one of those boolean properties.
I think what you're trying to do could be achieved with ItemsControl and DataTemplates (and maybe DataTemplateSelectors). This will allow you to store "data" in your ViewModel (say List or something more specific) without referencing the actual Controls and the relevant DataTemplates will add the right controls for different data types you have in your ViewModel.

how to validate a many fields in windows form

In my windows application i have a tab control with many tabs and sub tab controls also, each and every tab has many field like text boxes and list boxes.
i thought to validate every field by clicking button called "NEXT" before changing to nexttab and if any field fails with validation a message box should pop-up with error message and focus should remain in that field. can any one help me with ur suggestions...
validation requirements are required field and only numeric and alphanumeric...
Thanks in advance!!!!
A good way is to create a derived controls with input area and validation logic. Then, it is matter of enumerating hierarchy of controls and see if they are valid based on validation rules applied.
Another way, is to "attach" validation logic to controls. How? A mapping mechanism or use the "Tag" property.
But isn't it better to validate control using "Validate" event, when user tries to exit the control? That will not allow user to supply bunch of trash and hope it can be saved.

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?

Validation in Silverlight

how to validate the input form of a silverlight control?
I have 3 controls, two out of three are text boxes (For name, and Age), and the remaining one control is date picker.
When i hit submit button, the validation should be invoked. how it will done??
thanks in advance.
If you're using Silverlight 3 check out Data Validation. http://www.silverlightshow.net/items/Data-Validation-in-Silverlight-3.aspx
If you're using Silverlight 2 you'll have to roll your own code for validation.
I wrote my own validaion for SL2. It's based on:
Attached property to give control custom validation ID
Business object validators that identify invalid data
VisualTreeHelper to parse visual tree and match validation result and custom validation ID
Custom templates for controls in order to display validation
INotifyPropertyChanged to remove validation display if property value was changed.
You may also find the Validation Application Block (which is part of the Enterprise Library Silverlight Integration Pack) useful. It's in public preview right now.

Categories