开发者

How to make the content of Expander visible to VisualTreeHelper

I've put an Expander onto a Page. Within the expander there is a Label. After the page is loaded, the expander is not yet expanded. At this point, I inspect the Expander using VisualTreeHelper, I got a structure like this:

  Expander
    Border
      DockPanel
        开发者_JAVA百科ToggleButton
          Border
            Grid
              Ellipse
              Ellipse
              Path
              ContentPresenter
        ContentPresenter

where there is no Label under the last ContentPresenter.

However, Once the expander is expanded and then collapsed, I got the a different structure using VisualTreeHelper:

  Expander
    Border
      DockPanel
        ToggleButton
          Border
            Grid
              Ellipse
              Ellipse
              Path
              ContentPresenter
        ContentPresenter
          Label
            Border
              ContentPresenter

This time, the Label appears under the ContentPresenter.

It seems that as long as the expander has been expanded, VisualTreeHelper will know the contents within the Expander. But is there a possible way to make VisualTreeHelper be aware of Expander's contents without expanding it?

Thanks.


There is a method for forcing a control to load its contents programmatically.

I imagine you have some sort of recursive loop that searches through your elements. Before you call to get its children, use this call:

if (element is FrameworkElement)
     (element as FrameworkElement).ApplyTemplate()

That'll force it to apply its template and load the controls into memory. I imagine it is done this way for performance reasons, as controls are not normally needed until they are visible.

This approach also works for TabControl and any other control that can hide content, I believe.

Credit goes to Tao Liang who posted this solution over here. Read it also if you're having trouble with your recursive loop.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜