数据绑定后的 WPF TreeViewItem 标头值

发布于 2024-09-28 17:33:26 字数 1318 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流年里的时光 2024-10-05 17:33:26

好吧,经过一番搜索,我找到了解决该问题的充分方法。

使用以下代码,您可以在模板中找到命名项目:

if (treeViewItem != null)
{
        //Get the header content presenter.
        ContentPresenter header = treeViewItem.Template.FindName("PART_Header", treeViewItem) as ContentPresenter;

        if (header != null)
        {
            //Find a CheckBox called "checkBoxName"
            CheckBox cb = treeViewItem.HeaderTemplate.FindName("checkBoxName", header) as CheckBox;
        }
} 

另外,为了其他可能不太了解数据绑定树视图的人的利益:我在问题中发布的模板不是绑定a的正确方法树视图。对树的每个级别使用 HierarchicalDataTemplate。 HierarchicalDataTemplate 的直接内容将指定每个子树的标头内容,并且设置 ItemsSource 和 ItemTemplate 属性将允许您绑定和格式化子树子树,例如:

<HierarchicalDataTemplate x:Key="templateName" ItemsSource="{Binding Path=someCollection}" ItemTemplate="{StaticResource someOtherTemplate}">
    <TextBlock Text="{Binding Path=SomeProperty}" />
</HierarchicalDataTemplate>

我希望其他人会发现此信息有用。

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:

if (treeViewItem != null)
{
        //Get the header content presenter.
        ContentPresenter header = treeViewItem.Template.FindName("PART_Header", treeViewItem) as ContentPresenter;

        if (header != null)
        {
            //Find a CheckBox called "checkBoxName"
            CheckBox cb = treeViewItem.HeaderTemplate.FindName("checkBoxName", header) as CheckBox;
        }
} 

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:

<HierarchicalDataTemplate x:Key="templateName" ItemsSource="{Binding Path=someCollection}" ItemTemplate="{StaticResource someOtherTemplate}">
    <TextBlock Text="{Binding Path=SomeProperty}" />
</HierarchicalDataTemplate>

I hope someone else will find this information useful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文