朱利安,
这是一个非常好的问题。你为什么不尝试编写自己的树视图项目? :)我的意思是,不是从头开始,只是从现有的TreeViewItem派生并添加属性。我准备了一个快速样本,但随意随意修改它(如果有些事情不完全清楚,请提问)。这里我们去:
public class TreeViewItem_CustomControl : TreeViewItem
{
static TreeViewItem_CustomControl()
{
HasChildrenProperty = DependencyProperty.Register("HasChildren", typeof(Boolean), typeof(TreeViewItem_CustomControl));
}
static DependencyProperty HasChildrenProperty;
public Boolean HasChildren
{
get
{
return (Boolean)base.GetValue(HasChildrenProperty);
}
set
{
if (value)
{
if (this.Items != null)
{
this.Items.Add(String.Empty); //Dummy item
}
}
else
{
if (this.Items != null)
{
this.Items.Clear();
}
}
base.SetValue(HasChildrenProperty, value);
}
}
}
这是您的自定义TreeViewItem的代码。现在,让我们用它在XAML:
<TreeView>
<TreeViewItem Header="qwer">
Regulat tree view item.
</TreeViewItem>
<CustomTree:TreeViewItem_CustomControl x:Name="xyz" Header="temp header" Height="50">
<TreeViewItem>Custom tree view item, which will be removed.</TreeViewItem>
</CustomTree:TreeViewItem_CustomControl>
</TreeView>
正如你所看到的,第一个项目是一个普通的一个,第二个是你的自定义一个。请注意,它有一个孩子。接下来,你可以HasChildren属性绑定到一些布尔对象在你的视图模型或只是简单地通过设置HasChildren从代码测试我的自定义类来假上述XAML背后:现在
xyz.HasChildren = false;
,尽管你的元素有一个孩子,展开按钮不显示,所以这意味着,我的自定义类工作。
我希望我帮你,但随时问,如果你有任何问题。
Piotr。