Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF HierarchicalDataTemplate TreeView is not Clickable

I have a TreeView with a HierarchicalDataTemplate. The items are filled correctly but I can't click on the TreeView items. (I can't select one, so that is marked blue). I can click in front of the TreeViewItem and then the selected is marked blue. It looks like there is a small box that I can clicked but the not the rest.

Here is my code:

XAML:

<TreeView ItemsSource="{Binding Main.TreeItems}">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Path=Children}">
            <TreeViewItem Header="{Binding Path=Header}"/>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

Model

public class ITreeItem
{
    public string Header { get; set; }
    public List<ITreeItem> Children { get; set; } 
}

class MainModel : INotifyPropertyChanged
{
    private List<ITreeItem> _treeitems;

    public MainModel()
    {
        _treeitems = new List<ITreeItem>();

        List<ITreeItem> treeList = new List<ITreeItem>();

        ITreeItem myItem1 = new ITreeItem();
        myItem1.Header = "Test1";
        myItem1.Children = new List<ITreeItem>();
        treeList.Add(myItem1);

        myItem1.Header = "Test2";
        myItem1.Children = new List<ITreeItem>();
        treeList.Add(myItem1);

        TreeItems = treeList;          
    }

    public List<ITreeItem> TreeItems
    {
        get
        {
            return _treeitems;
        }
        set
        {
            _treeitems = value;
            OnPropertyChanged("TreeItems");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
like image 624
user2025830 Avatar asked Feb 14 '13 14:02

user2025830


1 Answers

In your XAML, instead of using a <TreeViewItem> under the HierarchicalDataTemplate, try using another control, such as a TextBlock:

<TextBlock Text="{Binding Path=Header}"/>
like image 188
Mash Avatar answered Nov 11 '22 09:11

Mash