朱利安,
這是一個非常好的問題。你爲什麼不嘗試編寫自己的樹視圖項目? :)我的意思是,不是從頭開始,只是從現有的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。