Reputation: 2363
I have WPF application in .NET 3.5 using c#.
At the moment the application is using a lot of code-behind approach, it was developed before I started working on it, and I wanted to remove all that code-behind approach and use the WPF Validation for the TextBoxes. Currently Validation occurs when Control Lost it's focus i.e.
private void Control_LostFocus(object sender, RoutedEventArgs e)
Now I followed this tutorial on Validation, and after implementing the Validation in my application I get these results:
As you can see Validation is not picking up changes for the TextBox, but when I try this:
Validation is a success.
And it is also success when I do this:
Don't know why this is valid?
Here is xaml for my TextBoxes:
<TextBox
MaxLength="20"
VerticalAlignment="Top"
VerticalContentAlignment="Center"
Width="120" Template="{DynamicResource TextBoxControlTemplate}" TabIndex="6" >
<TextBox.Text>
<Binding Path="Order.HirerName" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<ExceptionValidationRule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
<!-- OLD VALIDATION METHODS -->
<!--GotFocus="Control_GotFocus"
KeyUp="Control_KeyUp"
LostFocus="Control_LostFocus"-->
<TextBox.BorderBrush>
<SolidColorBrush Color="Green"/>
</TextBox.BorderBrush>
</TextBox>
My Question is: Why are the results so different and inconsistent. Sometimes I have to click on another TextBox or any other control which can gain focus to force the validation.
Note
When I click on the TextBox next to Hirer Telephone validation doesn't work until I move focus in other part of the Page. The TextBox and TextBlocks are in StackPanel i.e.
StackPanel
|
|->TextBlock
|->TextBox
I hope this makes sense.
EDIT
Here is the HirerName Property:
public string HirerName
{
get { return _HirerName; }
set
{
if (!string.Equals(_HirerName, value, StringComparison.Ordinal))
{
_HirerName = value;
OnPropertyChanged(new PropertyChangedEventArgs("HirerName"));
}
}
}
EDIT 2:
Here are screen shots of what the situation is like:
no input, field invalid
Field should be valid but Validation fails :-(
Control has lost focus and validation occurs !?
Note:
When Control looses the focus the field on Order is updated again i.e. I set the break point on my property and it's being hit when Control losses the focus.
Upvotes: 3
Views: 1264
Reputation: 10401
Well, I still consider myself young, but I do not believe in magic.
At least not in the programming and computer science. If something happens then it happens only because someone makes it happen. You can't get something for nothing.
No-exception-in-viewmodel works when your property is Int or DateTime - something that should be converted from the string.
Your property is String - there is no way the binding can fail and throw the exception that the ExceptionValidationRule will handle, because any string property easily binds to any string property without any conversion.
WPF knows nothing about your requirements to HirerName correctness. So it allows anything to pass as valid value to the setter. That's your property that should actively react to any incorrect data.
So:
public string HirerName
{
get { return _HirerName; }
set
{
if (String.IsNullOrEmpty(value)
throw new ArgumentException("Null or empty");
if (value.Length < 3)
throw new ArgumentException("Too short");
if (!string.Equals(_HirerName, value, StringComparison.Ordinal))
{
_HirerName = value;
OnPropertyChanged(new PropertyChangedEventArgs("HirerName"));
}
}
}
About UnhandledException:
I've forgot one thing: WPF engine handling(actually partial mishandling) of exceptions with ExceptionValidationRule can cause some problems in the debug mode. There are no such problems in the standalone release app.
From this SO thread:
The solution is not so obvious nor well documented, but simple enough. The reason Visual Studio breaks for exceptions when running in debug mode is because it's configured that way.
In the Debug menu, select "Exceptions...". In this dialog you control how VS handles exceptions. Simply uncheck "User-unhandled" for "Common Language Runtime Exceptions", press OK and run your project again.
P.S.: If I remember any better way to deal with it, then I'll add it here.
Upvotes: 1