简体   繁体   English

generic.xaml中的数据模板如何自动应用?

[英]How can data templates in generic.xaml get applied automatically?

I have a custom control that has a ContentPresenter that will have an arbitrary object set as it content. 我有一个自定义控件,它有一个ContentPresenter,它将内容设置为任意对象。 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. 此对象对其类型没有任何约束,因此我希望此控件基于应用程序或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. 但我在generic.xaml中为某些类型定义了一些数据模板,我在其中定义了自定义控件样式,并且这些模板未自动应用。 Here is the generic.xaml : 这是generic.xaml:

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

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

If I set an object of type 'PredefinedType' as the content in the contentpresenter, the data template does not get applied. 如果我将“PredefinedType”类型的对象设置为contentpresenter中的内容,则不会应用数据模板。 However, If it works if I define the data template in the app.xaml for the application thats using the custom control. 但是,如果我在app.xaml中为使用自定义控件的应用程序定义数据模板,则它可以正常工作。

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. 我真的不能假设控件的用户将定义这个数据模板,所以我需要一些方法来将它与自定义控件联系起来。

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). Generic.xaml中声明的资源只有在应用于控件的模板(通常是StaticResource引用)直接引用时才会被引入。 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. 在这种情况下,您无法设置直接引用,因此您需要使用另一种方法将DataTemplate与ControlTemplate打包在一起。 You can do this by including them in a more local Resources collection, like ControlTemplate.Resources: 您可以通过将它们包含在更本地的Resources集合中来完成此操作,例如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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM