我有TreeView
一个HierarchicalDataTemplate
。项目已正确填写,但我无法单击TreeView
项目。(我不能选择一个,所以它被标记为蓝色)。我可以点击前面的TreeViewItem
,然后选中的是蓝色的。看起来有一个小框可以单击,但其余的不能。
这是我的代码:
XAML:
<TreeView ItemsSource="{Binding Main.TreeItems}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Path=Children}">
<TreeViewItem Header="{Binding Path=Header}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
模型
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));
}
}
}