I have a WPF application where there is a TabControl
, and within a certain tab there is a TextBox
with a validation rule.
The validation rule works fine, the converter works fine, the binding is ok too.
But there is a very annoying bug (probably with WPF itself) that happens when I do this:
- Type an invalid text (the validation occurs, the textbox gets a red border)
- Change to another tab
- Return to the tab with the textbox.
The validation completely stops working until I type a valid text, then it starts working again. Changing the text is not enough, it will only start working again if I enter a valid text.
How can I force a revalidation of the text when I enter the tab?
I already tried two solutions listed here, but they seem not to bring the red border:
- In an event
Selector.Selected
on the tab (checked that it's being called), addtxtName.GetBindingExpression(TextBox.TextProperty).UpdateSource();
- Use the
ValidatesOnTargetUpdated="True"
property in the validation rule
This is a brief description of the XAML, if needed:
<TabControl ...>
<TabItem ... />
<TabItem ...>
...
<TextBox Name="txtName" ...>
<TextBox.Text>
<Binding Path="..."
UpdateSourceTrigger="PropertyChanged"
Mode="OneWayToSource"
FallbackValue="5"
Converter="MyCustomConverterWorkingOk">
<Binding.ValidationRules>
<local:MyCustomValidationWorkingOk/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
</TabItem>
</TabControl>