I need to apply a style to the ToggleButton of a specific treeviewitem. How can I access the ToggleButton using the treeviewitem?
The treeviewitem is in a controlte开发者_运维百科mplate.
Many thanks
You could parse the VisualTree (with the help of VisualTreeHelper) for the TreeViewItem until you find the ToggleButton (first ToggleButton? the ToggleButton with a certain Name? etc) and then set its Style property to the style you want, something like:
public void RestyleToggleButton(TreeViewItem visual, Style new_style)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visual); i++)
{
Visual childVisual = (Visual)VisualTreeHelper.GetChild(visual, i);
if(childVisual is ToggleButton)
{
((ToggleButton)childVisual).Style = new_style;
}
}
}
(maybe even go recursively if the ToggleButton is not a direct children of the TreeViewItem (and probably it is not)).
精彩评论