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

发布于 2024-08-30 20:47:23 字数 682 浏览 4 评论 0原文

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

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

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

如果我将“PredefinedType”类型的对象设置为 contentpresenter 中的内容,则不会应用数据模板。但是,如果我在 app.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>

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

老子叫无熙 2024-09-06 20:47:23

仅当应用到控件的模板直接引用 Generic.xaml 中声明的资源(通常通过 StaticResource 引用)时,才会拉入这些资源。在这种情况下,您无法设置直接引用,因此您需要使用另一种方法将 DataTemplate 与 ControlTemplate 打包。您可以通过将它们包含在更本地的资源集合中来实现此目的,例如 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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文