views:

22

answers:

1

I have found the need to collapse a given WPF element or control when it is visually empty and I'm curious whether others have come across this and if so what type of solutions have worked.

My current case is one where I have a Border control that either will have a child element or not depending on various cases. When Border.Child is null or otherwise unset I would like to also set Border.Visibility to Collapsed. I've tried making a trigger to do this which seemed to be the most logical solution however it appears that Child is not a dependency property and so when I attempted this I got a runtime exception.

As a workaround for now I've bound to a different property in my DataContext which coupled with a ValueConverter gives me a resulting condition which I can use to achieve the desired result. I'm hoping though that somebody may be able to come up with some slick Attached Behavior or some other trick that may be able to simplify collapsing empty elements in WPF.

A: 

Have you considered restyling an Expander control? That already does what you're describing in some sense, especially if you style the header to go away so that you programmatically control the collapsed state.

Greg D
Instead of an Expander, you could template a ContentControl and use a Trigger with the HasContent property.
Kris
Along the same lines as the HasContent property I've found the ItemsControl.HasItems property is also useful.
jpierson