如何检索 ItemsControl 中项目的 DataTemplate(和特定对象)?
我已经看到了一个非常相似问题的解决方案,但它并没有转化为我的。 (即这篇文章:http://blogs.msdn.com/wpfsdk/archive/2007/04/16/how-do-i-programmatically-interact-with-template- generated-elements- part-ii.aspx)
我的 ItemsControl 绑定到一个可观察集合,该集合可以动态添加项目。
当我将项目添加到可观察集合时,模板化项目会在我的项目控件中正确呈现,但我不知道如何访问它。我的可观察集合更改了代码,我正在尝试访问有关的信息。我使用自定义 DataTemplateSelector 根据集合中项目的数据返回 3 个不同的 dataTemplate 之一。
以下是我的 ItemsControl XAML 的概述:
<ItemsControl Name="myItemsControl" ItemTemplateSelector="{StaticResource myTempSelector}">
<ItemsControl.Template>
<ControlTemplate TargetType="ItemsControl">
<ItemsPresenter/>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
我见过的解决方案建议使用 ItemContainerGenerator.ContainerFromItem(xxx)
在此示例中,他们总是查找有关 ListBox 或 ComboBox(继承自 ContentControl)的信息)。但是,当我调用(在我的代码隐藏中)myItemsControl.ItemContainerGenerator.ContainerFromItem(xxx)
时,我收到一个 ContentPresenter,而不是我期望的 ContentControl。
然后,当我尝试访问此 ContentPresenter 的 ContentTemplate 时,出现空对象异常。
我有预感,我其余的麻烦都会从那里降临。
我想要做的就是从新创建的控件中的数据模板中找到一个文本框,并为其提供焦点。
帮助! :-)
I have seen solutions to a very similar issue, yet it doesn't translate to mine. (Namely, this article: http://blogs.msdn.com/wpfsdk/archive/2007/04/16/how-do-i-programmatically-interact-with-template-generated-elements-part-ii.aspx)
My ItemsControl is bound to an observable collection, which can have items dynamically added to it.
When I add an item to the observable collection, the templated item renders properly in my itemscontrol, but I can't figure out how to access it. My my observable colleciton changed code, I am trying to access information about. I am using a custom DataTemplateSelector to return one of 3 different dataTemplates, based on the item's data in the collection.
Here is an outline of my ItemsControl XAML:
<ItemsControl Name="myItemsControl" ItemTemplateSelector="{StaticResource myTempSelector}">
<ItemsControl.Template>
<ControlTemplate TargetType="ItemsControl">
<ItemsPresenter/>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
The solutions I've seen suggest using ItemContainerGenerator.ContainerFromItem(xxx)
In this examples, they are always looking for information about a ListBox or ComboBox (which inherit from ContentControl). However, when I call (in my code-behind) myItemsControl.ItemContainerGenerator.ContainerFromItem(xxx)
, I receive a ContentPresenter, rather than the ContentControl I expect.
Then, when I try to access the ContentTemplate of this ContentPresenter, I get a null object exception.
I have a hunch that the rest of my troubles descend from there.
All I want to do is find a textbox from the datatemplate in my newly created control, and give it focus.
Help! :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要获取 DataTemplate 本身的句柄,并使用其 FindName 方法,引用项目的父控件。
例如:
因此,这会在项目内找到一个名为“textBox1”的控件。
如果您不使用命名的 DataTemplate(即带有 x:Key="MyItemTemplate" 的 DataTemplate),而是使用 DataType="..." 来定义用于特定类型的 DataTemplate,则查找模板的方法略有变化:
You need to get a handle to the DataTemplate itself, and use its FindName method, referencing the parent control of your item.
For example:
So this finds a control called "textBox1" inside the item.
If you're not using a named DataTemplate (ie one with x:Key="MyItemTemplate") and instead using DataType="..." to define a DataTemplate to be used for specific types, the method by which you find the template changes slightly: