我怎样才能检索项目的一个ItemsControl中的DataTemplate(与特定的对象)? [英] How can I retrieve the DataTemplate (and specific objects) of an item in an ItemsControl?

查看:122
本文介绍了我怎样才能检索项目的一个ItemsControl中的DataTemplate(与特定的对象)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了解决方案,以一个非常类似的问题,但它并没有转化为我的。 (也就是说,这篇文章:<一个href="http://blogs.msdn.com/wpfsdk/archive/2007/04/16/how-do-i-programmatically-interact-with-template-generated-elements-part-ii.aspx" rel="nofollow">http://blogs.msdn.com/wpfsdk/archive/2007/04/16/how-do-i-programmatically-interact-with-template-generated-elements-part-ii.aspx)

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)

我的ItemsControl绑定到一个观察到的集合,其可具有动态添加到它的项目。

My ItemsControl is bound to an observable collection, which can have items dynamically added to it.

在我的项目添加到观察的集合,模板化项目正确呈现在我的ItemsControl,但我无法弄清楚如何访问它。我的我的观察到colleciton改变code,我试图获取有关信息。我使用的是自定义的DataTemplateSelector返回3个不同的DataTemplates之一,根据集合中的项目的数据。

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.

下面是我的ItemsControl XAML的轮廓:

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>

我见过的解决方案,建议使用 ItemContainerGenerator.ContainerFromItem(XXX)

在这个例子中,他们一直在寻找有关ListBox或ComboBox(其中自ContentControl继承)的信息。然而,当我打电话(我的code-后面) myItemsControl.ItemContainerGenerator.ContainerFromItem(XXX),我收到一条内容presenter,而不是ContentControl中我所期望的。

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.

然后,当我尝试访问该内容presenter的的ContentTemplate,我得到一个空对象异常。

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.

我想要做的就是找​​到从我的新创建的控制的DataTemplate一个文本框,并给予其焦点。

All I want to do is find a textbox from the datatemplate in my newly created control, and give it focus.

帮助! : - )

推荐答案

您需要获得一个句柄的DataTemplate本身,并使用其FindName方法,引用您的项目的父控件。

You need to get a handle to the DataTemplate itself, and use its FindName method, referencing the parent control of your item.

例如:

var item = myItemsControl.ItemContainerGenerator.ContainerFromItem(xxx);
var template = this.Resources["MyItemTemplate"] as DataTemplate;
var ctl = template.FindName("textBox1", item) as FrameworkElement;

所以这个发现一个叫项里面的textBox1的控制。

So this finds a control called "textBox1" inside the item.

如果你不使用一个命名的DataTemplate(即,一个与X:关键=MyItemTemplate),并使用替代数据类型=...来定义一个DataTemplate用于特定类型的方法,通过它您稍微找到模板更改:

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:

var actionKey = new DataTemplateKey(typeof(MyCustomClass));
var actionTemplate = Resources[actionKey] as DataTemplate;

这篇关于我怎样才能检索项目的一个ItemsControl中的DataTemplate(与特定的对象)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆