简体   繁体   中英

Get the height of a TreeViewItem's Header in WPF

I am trying to find the height of the header section of a TreeViewItem in order to draw an adorner. I have an instance of the TreeViewItem however, I am not sure how to get the VisualTreeRootNode for the header element in code. I am looking for something like:

FrameworkElement headerElement = 
     _myTreeViewItem.HeaderTemplate.GetVisualTreeRootNode();

How can I achieve this?

private FrameworkElement GetHeaderControl(TreeViewItem item)
{
   return (FrameworkElement)item.Template.FindName("PART_Header", item);
}

Many of the default templates are listed in examples on MSDN. For instance:

http://msdn.microsoft.com/en-us/library/ms788727.aspx

You can also see the name of the template part in the definition of the control here:

http://msdn.microsoft.com/en-us/library/system.windows.controls.treeviewitem(v=VS.100).aspx

As one of the Attributes attached to the class. This is a generalized mechanism for drilling down into templates on any well-defined WPF Control.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM