如何访问作为TreeItem的Datatemplate的编辑框 [英] How to access an edit box which is a Datatemplate of a TreeItem
问题描述
我有以下情况
有一个如下的DataTemplate
< DataTemplate x:键 = editableName >
< TextBox x:名称 = hTreeRenameBox / < span class =code-keyword>>
< / DataTemplate >
拥有如下树视图
<前lang =xml> < TreeView x:名称 = hScExTrview BorderBrush = Brown 背景 = #FF1E1E1E BorderThickness = 1 保证金 = 0,-1,-1,0 工具提示 = 会话脚本 >
< / TreeView >
现在我已经从Code为树视图创建了一些TreeItems。
我选择了一个点一个上下文菜单,它是树视图的一部分,我有下面的代码来添加DataTemplate与TreeView项目如下
TreeViewItem pp =(TreeViewItem)hScExTrview .SelectedItem;
String kk =(String)pp.Header;
pp.HeaderTemplate =(DataTemplate)this.Resources [editableName];
现在我的查询是我想获得在DataTemplate中声明的hTreeRenameBox。我如何从上面的C#代码中获得它。
我使用下面的代码,但是它不起作用;请帮助纠正我!
TextBox textBoxAnswer = pp.HeaderTemplate.FindName(hTreeRenameBox,pp)as TextBox;
您好Toufique。
您可以使用FrameworkTemplate的Find方法从模板中获取命名元素。
在你的情况下,你有一个问题,因为Header是由ContentPresenter代表的默认TreeViewItem ControlTemplate,如下所示:
< ContentPresenter x:名称 = PART_Header ContentSource = 标题 HorizontalAli gnment = {TemplateBinding HorizontalContentAlignment} SnapsToDevicePixels = {TemplateBinding SnapsToDevicePixels} / >
所以解决方案可以这样工作:
// 获取TreeViewItem使用的HeaderTemplate
FrameworkTemplate ft = pp.HeaderTemplate;
// 获取模板应用的FrameworkElement - 它是TreeViewItem模板中的ContentPresenter(如果你没有覆盖它..)
ContentPresenter presenterHeader = pp.Template.FindName( PART_Header ,pp) as ContentPresenter;
// 现在我们可以从应用它的FrameworkElement中找到模板的元素(ContentPresenter)
TextBox obj = ft.FindName( hTreeRenameBox,presenterHeader)< span class =code-keyword> as TextBox;
亲切的问候约翰内斯
PS也许你的WPF项目尝试适当的MVVM模式实现,摆脱你的代码隐藏和这样的问题?
Hi,
I have below situation
Have a DataTemplate like below
<DataTemplate x:Key="editableName">
<TextBox x:Name="hTreeRenameBox" />
</DataTemplate>
Have a treeview like below
<TreeView x:Name="hScExTrview" BorderBrush="Brown" Background="#FF1E1E1E" BorderThickness="1" Margin="0,-1,-1,0" ToolTip="Session Scripts">
</TreeView>
Now I have created some TreeItems for the treeview from Code.
One point I have selected one context menu which is part of the treeview and I have the below code to add the DataTemplate with TreeView Item like below
TreeViewItem pp = (TreeViewItem)hScExTrview.SelectedItem;
String kk = (String)pp.Header;
pp.HeaderTemplate = (DataTemplate)this.Resources["editableName"];
Now my query is I want to get "hTreeRenameBox" which is declared inside DataTemplate. How I can get that from the above C# code.
I used the below code but it is not working; kindly correct me!
TextBox textBoxAnswer = pp.HeaderTemplate.FindName("hTreeRenameBox",pp) as TextBox;
Hi Toufique.
You could use FrameworkTemplate's Find method to get a named element from a template.
In your case you have a "Problem" because the Header is in the default TreeViewItem ControlTemplate represented by a ContentPresenter like this:
<ContentPresenter x:Name="PART_Header" ContentSource="Header" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
So the solution could work like this:
// Get the HeaderTemplate used by the TreeViewItem FrameworkTemplate ft = pp.HeaderTemplate; // Get the FrameworkElement your Template is applied to - it's the ContentPresenter from your TreeViewItem Template (if you haven't overriden it..) ContentPresenter presenterHeader = pp.Template.FindName("PART_Header", pp) as ContentPresenter; // Now we can find the elements of the Template from the FrameworkElement it was applied to (the ContentPresenter) TextBox obj = ft.FindName("hTreeRenameBox", presenterHeader ) as TextBox;
Kind regards Johannes
P.S. maybe try a proper MVVM pattern implementation with your WPF Project, and get rid of your code-behind and such "problems"?
这篇关于如何访问作为TreeItem的Datatemplate的编辑框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!