WPF:获取 TreeViewItem 的组成控件
如果它们位于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这对我有用。毫无疑问,一如既往,有更好的方法,并且您无疑会添加额外的检查,例如检查子项计数和/或获取/检查循环中子项的类型/名称等。但基本技术是有效的,好吧尽管我有一个 Grid 而不是 StackPanel,但我在我的应用程序中做了。
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.
您可以使用 FrameworkTemplate.FindName 来查找在TreeView项控件模板中找到标题内容呈现器,然后再次在数据模板中找到您想要的部分。
您还可以使用 上的方法手动遍历可视化树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.
You could also walk the visual tree manually with the methods on VisualTreeHelper.
我假设这在 WPF 中与 silverlight 相同(这是 silverlight 版本)
I'm assuming that this will be the same in WPF as silverlight (this is the silverlight version)
上述解决方案均不适用于 Silverlight
但这似乎有效。
遵循代码
None of the above Solutions works in Silverlight
but this seems to work.
Following the code