在树视图中使用扩展器 [英] Using expander in treeview
问题描述
基本上我需要扩展器控制的东西。
我有一个主扩展器。它下面有很多部分。我想要的是将这些部分放在树视图中。每个部分也有自己的扩展器。主扩展器控制所有。这意味着如果它崩溃,所有部分都应该折叠,反之亦然。
更改部分是我要设置背景。我想扩展器的背景是不同的。当部分扩展时,这些部分的背景与主扩展器不同。
我尝试过:
我尝试使用模板来自定义扩展器,但不确定如何使用样式触发背景。我在网上找不到类似的例子。需要帮助。
Basically I need something with expander control.
I have a main expander. Under it it has many sections. What I want is to put the sections in a treeview. Each section also has its own expander. The main expander controls all. Which means if it collapses, all sections should be collapsed, vice versa.
The changeling part is that I want to set the background. I want the background of the expanders are different. When sections expand, the background of the sections are different from the main expander.
What I have tried:
I tried to use template to customize the expanders, but not sure how to trigger the background with the styles. I can't find the similar example online. Need help.
推荐答案
您可以使用VisualTreeHelper:
You can use the VisualTreeHelper:
private static IEnumerable<T> FindVisualChildren<T>(DependencyObject root) where T : DependencyObject
{
if (root != null)
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(root); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(root, i);
if (child is T) yield return (T)child;
foreach (T childOfChild in FindVisualChildren<T>(child))
yield return childOfChild;
}
}
使用以下
use the following
var treeItems = FindVisualChildren<treeviewitem> (this),ToList();
treeItems.ForEach(I => i.IsExpanded = false);
这篇关于在树视图中使用扩展器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!