WPF:获取 TreeViewItem 的组成控件

发布于 2024-09-09 07:56:34 字数 667 浏览 1 评论 0原文

如果它们位于 hierarichicaldatatemplate 内,如何在代码中获取组成 TreeViewItem 的组成控件?

<HierarchicalDataTemplate DataType="{x:Type local:Module}" ItemsSource="{Binding Children}">
    <StackPanel Orientation="Horizontal">
        <Image Width="16" Height="16" Margin="3,0" Source="Images\module.ico" />
        <local:RenamingNode Name="RenamableNode" Text="{Binding Name}" VstaObject="{Binding BindsDirectlyToSource=True}" OnRename="OnRenameOccured"  />
    </StackPanel>
</HierarchicalDataTemplate>

因此,以编程方式,当我获取以 TreeViewItem 作为源的事件时,我希望能够获取 local:RenamingNode,但无法获取 TreeViewItem 的后代。

谢谢,

伊利亚

how can I get the constituent controls making up the TreeViewItem in code if they are inside a hierarichicaldatatemplate?

<HierarchicalDataTemplate DataType="{x:Type local:Module}" ItemsSource="{Binding Children}">
    <StackPanel Orientation="Horizontal">
        <Image Width="16" Height="16" Margin="3,0" Source="Images\module.ico" />
        <local:RenamingNode Name="RenamableNode" Text="{Binding Name}" VstaObject="{Binding BindsDirectlyToSource=True}" OnRename="OnRenameOccured"  />
    </StackPanel>
</HierarchicalDataTemplate>

So programatically when I get an event with a TreeViewItem as the source, I want to be able to get the local:RenamingNode, but I can't get the TreeViewItem's descendants.

Thanks,

Ilya

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

<逆流佳人身旁 2024-09-16 07:56:34

这对我有用。毫无疑问,一如既往,有更好的方法,并且您无疑会添加额外的检查,例如检查子项计数和/或获取/检查循环中子项的类型/名称等。但基本技术是有效的,好吧尽管我有一个 Grid 而不是 StackPanel,但我在我的应用程序中做了。

private object FindContentTemplatePart(TreeViewItem treeViewItem) 
{ 
    if (treeViewItem != null) 
    { 
        var header = (ContentPresenter)treeViewItem.Template.FindName("PART_Header", treeViewItem);

        if (header != null) 
        { 
            StackPanel stackPanel = (StackPanel)VisualTreeHelper.GetChild(header,0);

            return stackPanel.Children[2];
        } 
    } 
    return null; 
} 

This worked for me. Doubtless there is a better way, as always, and you will doubtless add extra checks such as checking the Child(ren) count and/or getting/checking type/name of children in a loop etc. but the basic technique works, well it did in my app although I have a Grid instead of a StackPanel.

private object FindContentTemplatePart(TreeViewItem treeViewItem) 
{ 
    if (treeViewItem != null) 
    { 
        var header = (ContentPresenter)treeViewItem.Template.FindName("PART_Header", treeViewItem);

        if (header != null) 
        { 
            StackPanel stackPanel = (StackPanel)VisualTreeHelper.GetChild(header,0);

            return stackPanel.Children[2];
        } 
    } 
    return null; 
} 
我早已燃尽 2024-09-16 07:56:34

您可以使用 FrameworkTemplate.FindName 来查找在TreeView项控件模板中找到标题内容呈现器,然后再次在数据模板中找到您想要的部分。

private object FindContentTemplatePart(TreeViewItem treeViewItem)
{
    if (treeViewItem != null)
    {
        var header = treeViewItem.Template.FindName("PART_Header", treeViewItem) as ContentPresenter;
        if (header != null)
        {
            return header.ContentTemplate.FindName("RenamableNode", header);
        }
    }
    return null;
}

您还可以使用 上的方法手动遍历可视化树VisualTreeHelper

You can use FrameworkTemplate.FindName to find the header content presenter in the TreeView item control template, and then again to find the part you want in the data template.

private object FindContentTemplatePart(TreeViewItem treeViewItem)
{
    if (treeViewItem != null)
    {
        var header = treeViewItem.Template.FindName("PART_Header", treeViewItem) as ContentPresenter;
        if (header != null)
        {
            return header.ContentTemplate.FindName("RenamableNode", header);
        }
    }
    return null;
}

You could also walk the visual tree manually with the methods on VisualTreeHelper.

毅然前行 2024-09-16 07:56:34

我假设这在 WPF 中与 silverlight 相同(这是 silverlight 版本)

(treeViewItem.HeaderTemplate.LoadContent() as StackPanel).FindName("RenamableNode")

I'm assuming that this will be the same in WPF as silverlight (this is the silverlight version)

(treeViewItem.HeaderTemplate.LoadContent() as StackPanel).FindName("RenamableNode")
山川志 2024-09-16 07:56:34

上述解决方案均不适用于 Silverlight
但这似乎有效。

<common:HierarchicalDataTemplate  x:Key="myHierarchicalTemplate"  ItemsSource="{Binding Children}"  >
        <StackPanel x:Name="spTreeItem"  Height="23" Margin="0,0,0,0" HorizontalAlignment="Stretch" Orientation="Horizontal">

        </StackPanel>
    </common:HierarchicalDataTemplate>

遵循代码

 TreeViewItem item = TreeViewWorkarounds.ContainerFromItem(trtFolders, trtFolders.SelectedItem);
 Grid ItemGrid = (Grid) VisualTreeHelper.GetChild(item, 0);
 Button ItemGridButton = (Button)VisualTreeHelper.GetChild(ItemGrid, 2);
 Grid ButtonGrid = (Grid)VisualTreeHelper.GetChild(ItemGridButton, 0);
 ContentPresenter CP = (ContentPresenter)VisualTreeHelper.GetChild(ButtonGrid, 1);
 ContentPresenter CPchlild = (ContentPresenter)VisualTreeHelper.GetChild(CP, 0);
 StackPanel sp = (StackPanel)VisualTreeHelper.GetChild(CPchlild, 0);

None of the above Solutions works in Silverlight
but this seems to work.

<common:HierarchicalDataTemplate  x:Key="myHierarchicalTemplate"  ItemsSource="{Binding Children}"  >
        <StackPanel x:Name="spTreeItem"  Height="23" Margin="0,0,0,0" HorizontalAlignment="Stretch" Orientation="Horizontal">

        </StackPanel>
    </common:HierarchicalDataTemplate>

Following the code

 TreeViewItem item = TreeViewWorkarounds.ContainerFromItem(trtFolders, trtFolders.SelectedItem);
 Grid ItemGrid = (Grid) VisualTreeHelper.GetChild(item, 0);
 Button ItemGridButton = (Button)VisualTreeHelper.GetChild(ItemGrid, 2);
 Grid ButtonGrid = (Grid)VisualTreeHelper.GetChild(ItemGridButton, 0);
 ContentPresenter CP = (ContentPresenter)VisualTreeHelper.GetChild(ButtonGrid, 1);
 ContentPresenter CPchlild = (ContentPresenter)VisualTreeHelper.GetChild(CP, 0);
 StackPanel sp = (StackPanel)VisualTreeHelper.GetChild(CPchlild, 0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文