On button click I'm validating if value in the TextBox is valid:
txtName.GetBindingExpression(TextBox.TextProperty).UpdateSource();
And text box is highlighted with red border and error tip near.
Then I switch to other tab. Come back - TextBox is not highlighted any more. On button click I validate again, and validation rule works fine and returns false, but the textbox is still not highlighted.
How to highlight TextBox as invalid again?
Validation rule:
public class TextRequired : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
var text = value as string;
return new ValidationResult(!string.IsNullOrEmpty(text), "Please, enter alue");
}
}
The validation error adorner (the red rectangle) is being displayed in an adorner layer that belongs to some ancestor to the TabControl. When you switch tabs, the original tab's content is unloaded, and at that point, the adorner gets removed from that adorner layer, as the adorner and its adorned element are no longer in the same visual tree. However, when you switch back to the original tab, the adorner isn't being added again. This is a weakness of WPF's validation adorner support. I don't know if it's by design or oversight, but the solution is pretty simple.
Simply wrap your tab item's content in an AdornerDecorator. That will force the error adorner to be added to an adorner layer within the tab, where it will remain even while the tab's content is unloaded. When you switch back, it will still be there.
<TabItem>
<AdornerDecorator>
<!-- tab content -->
</AdornerDecorator>
</TabItem>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With