数据绑定后的 WPF TreeViewItem 标头值
我有一个 TreeView,其内容(嵌套的 TreeViewItems)是通过数据绑定从数据集生成的,这一切似乎都工作正常。我遇到的问题是,当我尝试在代码中操作 TreeViewItem 标头的内容时,Header 属性返回生成 TreeViewItem 的 DataRowView,而不是如我所期望的那样,返回由模板生成的控件。
这是我用来生成 TreeViewItems 的模板示例:
<DataTemplate x:Key="seasonTreeViewItemTemplate">
<TreeViewItem>
<TreeViewItem.Header>
<CheckBox Content="{Binding Path=Row.SeasonID}" Tag="{Binding}" ToolTip="{Binding Path=Row.Title}" IsEnabled="{StaticResource seasonPermitted}" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" />
</TreeViewItem.Header>
<TreeViewItem Header="Championships" ItemTemplate="{StaticResource championshipTreeViewItemTemplate}">
<TreeViewItem.ItemsSource>
<Binding Path="Row" ConverterParameter="FK_Championship_Season">
<Binding.Converter>
<local:RowChildrenConverter />
</Binding.Converter>
</Binding>
</TreeViewItem.ItemsSource>
</TreeViewItem>
</TreeViewItem>
</DataTemplate>
任何人都可以指出我出错的地方并建议我如何访问标题复选框(如果可能的话,最好不要深入研究 VisualTree)?
谢谢, 詹姆斯
I have a TreeView whose contents (nested TreeViewItems) are generated from a dataset via databinding, which all seems to work fine. The issue I'm running into is that when I try and manipulate the contents of the TreeViewItem headers in code, the Header property returns the DataRowView that the TreeViewItem was generated from and not, as I was expecting, the control generated by the template.
Here's an example of the template I'm using to generate the TreeViewItems:
<DataTemplate x:Key="seasonTreeViewItemTemplate">
<TreeViewItem>
<TreeViewItem.Header>
<CheckBox Content="{Binding Path=Row.SeasonID}" Tag="{Binding}" ToolTip="{Binding Path=Row.Title}" IsEnabled="{StaticResource seasonPermitted}" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" />
</TreeViewItem.Header>
<TreeViewItem Header="Championships" ItemTemplate="{StaticResource championshipTreeViewItemTemplate}">
<TreeViewItem.ItemsSource>
<Binding Path="Row" ConverterParameter="FK_Championship_Season">
<Binding.Converter>
<local:RowChildrenConverter />
</Binding.Converter>
</Binding>
</TreeViewItem.ItemsSource>
</TreeViewItem>
</TreeViewItem>
</DataTemplate>
Can anyone point out where I'm going wrong and advise me how to access the header checkboxes (ideally without delving into the VisualTree if possible)?
Thanks,
James
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,经过一番搜索,我找到了解决该问题的充分方法。
使用以下代码,您可以在模板中找到命名项目:
另外,为了其他可能不太了解数据绑定树视图的人的利益:我在问题中发布的模板不是绑定a的正确方法树视图。对树的每个级别使用 HierarchicalDataTemplate。 HierarchicalDataTemplate 的直接内容将指定每个子树的标头内容,并且设置 ItemsSource 和 ItemTemplate 属性将允许您绑定和格式化子树子树,例如:
我希望其他人会发现此信息有用。
Well, after some searching I have found an adequate solution to the problem.
Using the following code, you can find named items in the template:
Also, for the benefit of anyone else who may not be too clued up on databinding treeviews: The template I posted in my question is not the right way to go about binding a treeview. Use a HierarchicalDataTemplate for each level of the tree. The direct content of the HierarchicalDataTemplate will specify the header content of each subtree and setting the ItemsSource and ItemTemplate properties will allow you to bind and format the subtrees children, for example:
I hope someone else will find this information useful.