Get the Height of the WPF Expander Header
Asked Answered
B

2

6

I need to get the Height of the WPF Expander.Header, not the whole Expander just the Height of the Header.

There is no property to get it because the Expander.Header + Expander.Content is the Expander.Height.

What would you do to get the Expander.Header Height ?

Borate answered 25/12, 2010 at 21:57 Comment(0)
A
5

If your expander isn't templated, that's a Visual tree:

Expander { Border { DockPanel { ToggleButton, ContentPresenter {...} } } }

All you need is to get that ToggleButton. It's easy using VisualTreeHelper:

var border = VisualTreeHelper.GetChild(expander, 0);
var dockpanel = VisualTreeHelper.GetChild(border, 0);
var togglebutton = VisualTreeHelper.GetChild(dockpanel, /*0*/); // it may be not 0th, so please enumerate all children using VisualTreeHelper.GetChildrenCount(dockpanel) and find that ToggleButton
return togglebutton.ActualHeight;

Edit

Also, I'd like to accent on using ActualHeight, not Height, because Height is not double.IsNaN (in XAML, auto) only if set explicitly in code or XAML

Android answered 25/12, 2010 at 22:25 Comment(2)
same I found on msdn forum now looks ok :) prolly you found this link too? :P => social.msdn.microsoft.com/Forums/en/wpf/thread/…Borate
No, I didn't. Just investigated the visual tree - that's first what I do when some info about element's structure is neededAndroid
T
3

I don't know of a way to do that exactly (maybe through reflection?), but you could try using two expanders. One with just a header and one with just a ContentPresenter. You could bind the IsExpanded property of the first expander to the IsExpanded property of the second one. This would make them appear to be a single expander.

I'm not sure exactly what you're trying to accomplish though.

Trejo answered 25/12, 2010 at 22:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.