开发者

Access the ToggleButton from a TreeviewItem

开发者 https://www.devze.com 2023-02-07 01:23 出处:网络
I need to apply a style to the ToggleButton of a specific treeviewitem. How can I access the ToggleButton using the 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

精彩评论

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