WPF expanders and error providers

Task: Add a red error box around a textbox when it fails validation.

Problem: Expanders in WPF hide the contents making them not visible when collapsed. The AdornedElementPlaceholder is rendered on its own layer that is NOT contained within the Expander. This means when you collapse the expander while you have any kind of error shown (even if you customise etc) then the textbox disappears and the error does not.

Workaround: The best I could do was use my own ControlTemplate, hide the AdornedElementPlaceholder within a DockPanel and then use a trigger to change the textbox’s border to red, indicating an error. Because I’m changing the actual textbox there is no problems with it being hidden when required by the expander.

Update: Originally I posted XAML here but blogger thought it was html so will have to figure out how to post XAML. doh!
There was a code post converter somewhere… hmmm that might do the trick

Of course if anyone has a better solution (some way to bind the Visibility of the dockpanel to the IsVisible of the textbox for example? Tried it… wouldnt bind, different parents?) please let me know.

Posted in WPF

3 thoughts on “WPF expanders and error providers

  1. Try adding an AdornerDecorator around the TextBox.Expander \ AdornerDecorator \ TextBox The schematic representation shown above is due to the inability to add xaml markup to this comment.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s