5

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));
        }
    }
}
Gromy
  • 258
  • 1
  • 15
user2025830
  • 872
  • 2
  • 17
  • 37
  • Amazing, some examples use the TreeViewItem in this way from what I have seen. I've been stuck for ages with the same issue. – Andez May 19 '14 at 13:12

2 Answers2

12

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

<TextBlock Text="{Binding Path=Header}"/>
Mash
  • 1,496
  • 13
  • 17
  • thanks. that works well. is there a waya to expand the items automatically in xaml? – user2025830 Feb 14 '13 at 15:00
  • @user2025830 The way I would do it is create an `bool IsExpanded` Property in your `ITreeItem` class, have the initial value set to `True`, and have that class implement `INotifyPropertyChanged` as well. Then in your `Resources` have a `Style` that targets the `TreeViewItem`. – Mash Feb 14 '13 at 15:08
  • @user2025830 Here is the style: – Mash Feb 14 '13 at 15:08
-1

The previous solution avoids the problem. There is a way to use header to select a TreeViewItem: on the MSDN website we can find an example which uses header and where TreeViewItem are clickable. Does someone have an idea why here it's not possible?

I personally hacked that using MouseButtonEventHandler adding a foreach on items with isSelected = false; and then ((TreeViewItem)sender).IsSelected = true; but that's dirty.

Gromy
  • 258
  • 1
  • 15
Fla
  • 536
  • 6
  • 23
  • 3
    It's because the OP is using an `ItemTemplate`. When you use an ItemTemplate, a is implicitly created. This explains why TreeViewItems are still generated even though a `TextBlock` is used. It is common practice to use a TextBlock to render TreeViewItems which use templates. – Mash Apr 11 '14 at 16:52