WPF自定义控件 - 没有被应用的ItemsControl模板 [英] WPF Custom Control - ItemsControl template not being applied

查看:692
本文介绍了WPF自定义控件 - 没有被应用的ItemsControl模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建设,从TabControl的派生的自定义WPF控件。在ControlTemplate中,我使用的ItemsControl来显示正在从模板(类型FileMenuItem可观察到的集合)的约束列表。程序执行过程中,我发现了以下错误在输出窗口:

I'm building a custom WPF control that derives from TabControl. In the ControlTemplate, I'm using a ItemsControl to display a list that is being bound from the template (an observable collection of type FileMenuItem). During program execution, I'm getting the following error in the output window:

的ItemTemplate和ItemTemplateSelector
被忽略的项目已的
的ItemsControl的容器类型;
类型='FileMenuItem

ItemTemplate and ItemTemplateSelector are ignored for items already of the ItemsControl's container type; Type='FileMenuItem'

该型FileMenuItem从菜单项的。如果我改变了基类的DependencyObject,代码实际运行和应用模板(所以这是一个选项)。我一派错误,但没有找到任何关于它,有没有人碰到这个在开发自定义控件?即使我有一个解决办法,我想知道发生了什么,我想使用菜单项作为基类是一个更清洁的实现。

The type FileMenuItem is derived from MenuItem. If I change the base class to DependencyObject, the code actually runs and the template is applied (so that's an option). I googled the error and couldn't find anything about it, has anyone run into this while developing custom controls? Even though I have a workaround, I'd like to understand what's happening, and I think using the MenuItem as a base class is a cleaner implementation.

我可以发布更多代码,如果这将有助于。谢谢!

I can post more code if it would help. Thanks!

推荐答案

一个DataTemplate(像ItemTemplate中)的目的,是提供一种用于数据对象的可视化。具体地,它定义了一组元素的添加到视件树代替给一个ContentPresenter或ItemsPresenter数据。在你的情况你的源列表是已经可以直接加入到在UI显示的可视化树对象的集合。

The purpose of a DataTemplate (like ItemTemplate) is to provide a visualization for a data object. Specifically, it defines a set of elements to add to the visual tree in place of the data given to an ContentPresenter or ItemsPresenter. In your case your source list is a collection of objects that are already able to be added directly to the visual tree for display in the UI.

您可以在看到这下面简单的例子,其中仅三公红色显示,因为前两个项目都可以直接通过组合框来显示一个表单定义。

You can see this in the following simplified example where only "Three" shows up in Red because the first two items are defined in a form that can be displayed directly by ComboBox.

<ComboBox>
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}" Foreground="Red"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
    <ComboBoxItem>One</ComboBoxItem>
    <ComboBoxItem>Two</ComboBoxItem>
    <sys:String>Three</sys:String>
</ComboBox>

这篇关于WPF自定义控件 - 没有被应用的ItemsControl模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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