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 ?

Jason Down
  • 21,731
  • 12
  • 83
  • 117
Elisabeth
  • 20,496
  • 52
  • 200
  • 321

2 Answers2

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

Mykola Bohdiuk
  • 1,307
  • 12
  • 16
  • 1
    same I found on msdn forum now looks ok :) prolly you found this link too? :P => http://social.msdn.microsoft.com/Forums/en/wpf/thread/a90b752a-6200-4542-8ec8-29cb902b8aa7 – Elisabeth Dec 26 '10 at 21:07
  • No, I didn't. Just investigated the visual tree - that's first what I do when some info about element's structure is needed – Mykola Bohdiuk Dec 26 '10 at 21:15
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.

Jason Down
  • 21,731
  • 12
  • 83
  • 117