WPF HierarchicalDataTemplate TreeView is not viewable

I have TreeView

c HierarchicalDataTemplate

. The items are filled in correctly, but I cannot click on the items TreeView

. (I can't pick one, so it's blue). I can click in front TreeViewItem

and then selected in blue. It looks like there is a small box that I can click, but not all.

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));
        }
    }
}

      

+3


source to share


2 answers


In your XAML, instead of using <TreeViewItem>

pod, HierarchicalDataTemplate

try using a different control like TextBlock

:



<TextBlock Text="{Binding Path=Header}"/>

      

+10


source


The previous solution avoids the problem. There is a way to use a header for selection TreeViewItem

: on the MSDN website we can find an example that uses a header and where are TreeViewItem

clickable. Does anyone have an idea why this is not possible here?



I personally hacked this by using MouseButtonEventHandler

adding foreach

to elements with isSelected = false;

and then ((TreeViewItem)sender).IsSelected = true;

, but this is messy.

0


source







All Articles