开发者

How to dynamically collapse a WPF element such as Border when it has no child element?

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开发者_Go百科 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜