Julian,
This is a really good question. Why don't you try writing your own tree element? :) I mean, not from scratch, just get from the existing TreeViewItem and add your property. I have prepared a quick sample, but feel free to modify it as you wish (and ask questions if something is not entirely clear). Here we go:
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);
This was the code for your custom TreeViewItem. Now let use it in 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>
As you can see, the first element is ordinary, and the second is your ordinary. Please note that he has one child. You can then bind the HasChildren property to some boolean in your ViewModel or just simply test my own class by setting HasChildren to False from the code located above the aforementioned XAML:
xyz.HasChildren = false;
Now, despite the fact that your element has one child, the extension button does not appear, so this means that my custom class is working.
Hope I helped you, but feel free to ask if you have any questions.
Peter.
source share