我的树定义是:
<TreeView Name="tree" ItemsSource="{Binding Children}" >
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}">
<CheckBox Name="foo"></CheckBox>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
有一个 TreeViewItem 元素,我尝试找到相应的 CheckBox,但是
tree.Template.FindName("foo", item);
投掷
[System.InvalidOperationException] = {"This operation is valid only on elements that have this template applied."}
和
item.Template.FindName("foo", item)
给我null。什么是正确的解决方案?