如何自动应用generic.xaml中的数据模板? [英] How can data templates in generic.xaml get applied automatically?

查看:107
本文介绍了如何自动应用generic.xaml中的数据模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有ContentPresenter的自定义控件,该控件将设置一个任意对象作为内容。该对象的类型没有任何限制,因此我希望此控件根据应用程序定义的任何数据模板或Generic.xaml中定义的数据模板显示其内容。如果在应用程序中定义了一些数据模板(没有键,因为我希望将其自动应用于该类型的对象),并且使用绑定到该类型对象的自定义控件,则该数据模板将自动应用。但是我在generic.xaml中为某些类型定义了一些数据模板,在其中定义了自定义控件样式,并且这些模板不会自动应用。这是generic.xaml:

I have a custom control that has a ContentPresenter that will have an arbitrary object set as it content. This object does not have any constraint on its type, so I want this control to display its content based on any data templates defined by application or by data templates defined in Generic.xaml. If in a application I define some data template(without a key because I want it to be applied automatically to objects of that type) and I use the custom control bound to an object of that type, the data template gets applied automatically. But I have some data templates defined for some types in the generic.xaml where I define the custom control style, and these templates are not getting applied automatically. Here is the generic.xaml :

<DataTemplate DataType="{x:Type PredefinedType>
    <!-- template definition -->
<DataTemplate>

<Style TargetType="{x:Type CustomControl}">
    <!-- control style -->
</Style>

如果我设置了类型为' PredefinedType作为contentpresenter中的内容,则不会应用数据模板。但是,如果我在使用自定义控件的应用程序的app.xaml中定义了数据模板,则该模板有效。

If I set an object of type 'PredefinedType' as the content in the contentpresenter, the data template does not get applied. However, If it works if I define the data template in the app.xaml for the application thats using the custom control.

有人知道吗?我真的不能假设控件的用户将定义此数据模板,所以我需要某种方式将其与自定义控件联系起来。

Does someone got a clue? I really cant assume that the user of the control will define this data template, so I need some way to tie it up with the custom control.

推荐答案

仅当应用到控件的模板直接引用了Generic.xaml中声明的资源时(通常通过StaticResource引用)。这种情况下您无法设置直接引用,因此您需要使用另一种方法将DataTemplates与ControlTemplate打包在一起。您可以通过将它们包含在更本地的Resources集合中来实现,例如ControlTemplate.Resources:

Resources declared in Generic.xaml are only pulled in if they are directly referenced by the template being applied to a control (usually by a StaticResource reference). In this case you can't set up a direct reference so you need to use another method to package the DataTemplates with your ControlTemplate. You can do this by including them in a more local Resources collection, like ControlTemplate.Resources:

<Style TargetType="{x:Type local:MyControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:MyControl}">
                <ControlTemplate.Resources>
                    <DataTemplate DataType="{x:Type local:MyDataObject}">
                        <TextBlock Text="{Binding Name}"/>
                    </DataTemplate>
                </ControlTemplate.Resources>
                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"
                        Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}">
                    <ContentPresenter/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这篇关于如何自动应用generic.xaml中的数据模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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