开发者

Access the ToggleButton from a TreeviewItem

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)).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜