如何检索 ItemsControl 中项目的 DataTemplate(和特定对象)?

发布于 2024-08-21 14:38:43 字数 1562 浏览 7 评论 0原文

我已经看到了一个非常相似问题的解决方案,但它并没有转化为我的。 (即这篇文章: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 技术交流群。

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

发布评论

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

评论(1

揪着可爱 2024-08-28 14:38:43

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

例如:

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

因此,这会在项目内找到一个名为“textBox1”的控件。

如果您不使用命名的 DataTemplate(即带有 x:Key="MyItemTemplate" 的 DataTemplate),而是使用 DataType="..." 来定义用于特定类型的 DataTemplate,则查找模板的方法略有变化:

var actionKey = new DataTemplateKey(typeof(MyCustomClass));
var actionTemplate = Resources[actionKey] as 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:

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

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:

var actionKey = new DataTemplateKey(typeof(MyCustomClass));
var actionTemplate = Resources[actionKey] as DataTemplate;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文