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.
UpdateSourceTrigger="LostFocus"but my question is about why it's not working correctly forUpdateSourceTrigger="PropertyChanged". - XAMlMAX