在 WPF 中,您可以像使用样式一样将一个 DataTemplate 建立在另一个 DataTemplate 之上吗?

发布于 2024-11-05 18:04:46 字数 2278 浏览 3 评论 0原文

可能的重复:
数据模板继承

我有几种不是子类的数据类型,它们也不共享接口,但它们确实有共同点我想要在 XAML DataTemplate 中显示的属性。也就是说,我知道这是可能的......

<!-- Basic Style Inheritance -->
<Style x:Key="FooStyle" TargetType="Foo" />
<Style x:Key="EnhancedFooStyle" TargetType="Foo" BasedOn="{StaticResource FooStyle}" />

<!-- Inheritance By Type -->
<Style x:Key="BaseItemStyle">
    <Setter Property="Control.Background" Value="Yellow" />
</Style>

<!-- These three data types share the same 'BaseItemStyle' -->
<Style TargetType="ListBoxItem" BasedOn="{StaticResource BaseItemStyle}" />
<Style TargetType="ComboBoxItem" BasedOn="{StaticResource BaseItemStyle}" />
<Style TargetType="TreeViewItem" BasedOn="{StaticResource BaseItemStyle}" />

但是我们可以对没有 BasedOn 属性的数据模板做类似的事情吗?

<DataTemplate x:Key="CommonTemplate">
    <!-- Common Stuff Goes Here -->
</DataTemplate>

<!-- These three datatypes share the same DataTemplate -->
<DataTemplate DataType="Foo1" BasedOn="{StaticResource CommonTemplate}" />
<DataTemplate DataType="Foo2" BasedOn="{StaticResource CommonTemplate}" />
<DataTemplate DataType="Foo3" BasedOn="{StaticResource CommonTemplate}" />

我知道 BasedOn 不是我们想要的,因为在这种情况下它不是“基于”而是“是”,但不确定如何纯粹在 XAML 中执行此操作。当我写这篇文章时,我有一个想法,但我觉得使用 UserControl 是作弊......

<UserControl x:Key="CommonTemplate" x:Shared="False">
    <!-- Common Stuff Goes Here -->
</UserControl>

<!-- These three datatypes share the same DataTemplate -->
<DataTemplate DataType="Foo1" BasedOn="{StaticResource CommonTemplate}">
    <StaticResource ResourceKey="CommonTemplate" />
</DataTemplate>

<DataTemplate DataType="Foo2" BasedOn="{StaticResource CommonTemplate}" />
    <StaticResource ResourceKey="CommonTemplate" />
</DataTemplate>

<DataTemplate DataType="Foo3" BasedOn="{StaticResource CommonTemplate}" />
    <StaticResource ResourceKey="CommonTemplate" />
</DataTemplate>

谢谢!

Possible Duplicate:
Datatemplate inheritance

I have several data types that are not subclasses, nor do they share an interface, but they do have common properties that I want to display in a XAML DataTemplate. That said, I know this is possible...

<!-- Basic Style Inheritance -->
<Style x:Key="FooStyle" TargetType="Foo" />
<Style x:Key="EnhancedFooStyle" TargetType="Foo" BasedOn="{StaticResource FooStyle}" />

<!-- Inheritance By Type -->
<Style x:Key="BaseItemStyle">
    <Setter Property="Control.Background" Value="Yellow" />
</Style>

<!-- These three data types share the same 'BaseItemStyle' -->
<Style TargetType="ListBoxItem" BasedOn="{StaticResource BaseItemStyle}" />
<Style TargetType="ComboBoxItem" BasedOn="{StaticResource BaseItemStyle}" />
<Style TargetType="TreeViewItem" BasedOn="{StaticResource BaseItemStyle}" />

But can we do something similar like this for data templates which don't have the BasedOn property?

<DataTemplate x:Key="CommonTemplate">
    <!-- Common Stuff Goes Here -->
</DataTemplate>

<!-- These three datatypes share the same DataTemplate -->
<DataTemplate DataType="Foo1" BasedOn="{StaticResource CommonTemplate}" />
<DataTemplate DataType="Foo2" BasedOn="{StaticResource CommonTemplate}" />
<DataTemplate DataType="Foo3" BasedOn="{StaticResource CommonTemplate}" />

I know BasedOn isn't what we want here because it's not 'based on' but rather 'is' in this scenario, but not sure how to do that purely in XAML. As I'm writing this, I have an idea, but I feel using UserControl is cheating...

<UserControl x:Key="CommonTemplate" x:Shared="False">
    <!-- Common Stuff Goes Here -->
</UserControl>

<!-- These three datatypes share the same DataTemplate -->
<DataTemplate DataType="Foo1" BasedOn="{StaticResource CommonTemplate}">
    <StaticResource ResourceKey="CommonTemplate" />
</DataTemplate>

<DataTemplate DataType="Foo2" BasedOn="{StaticResource CommonTemplate}" />
    <StaticResource ResourceKey="CommonTemplate" />
</DataTemplate>

<DataTemplate DataType="Foo3" BasedOn="{StaticResource CommonTemplate}" />
    <StaticResource ResourceKey="CommonTemplate" />
</DataTemplate>

Thanks!

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

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

发布评论

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

评论(1

尴尬癌患者 2024-11-12 18:04:46

@Foovanadil,实际上我想我想出了更好的东西。我的新方法不仅避免了额外的绑定(内容呈现器上的绑定),而且还消除了在显式设置其内容时由该呈现器应用模板的需要。这两件事都应该可以加快你的 UI 速度,尤其是在更大、更复杂的界面中。

<Border x:Shared="False" x:Key="Foo" BorderBrush="Red" BorderThickness="1" CornerRadius="4">
    <TextBlock Text="{Binding SomeProp}" />
</Border>

<DataTemplate x:Key="TemplateA">
    <ContentPresenter Content="{StaticResource Foo}" />
</DataTemplate>

<DataTemplate x:Key="TemplateB">
    <ContentPresenter Content="{StaticResource Foo}" />
</DataTemplate>

重要提示:请确保在共享内容上使用 x:Shared 属性,否则这将不起作用。

中号

@Foovanadil, actually I think I came up with something better. My new approach not only avoids the extra binding (the one on the content presenter), but also removes the need to have the template be applied at all by that presenter as you're explicitly setting its contents. Both of these things should speed up your UI, especially in larger, more complex interfaces.

<Border x:Shared="False" x:Key="Foo" BorderBrush="Red" BorderThickness="1" CornerRadius="4">
    <TextBlock Text="{Binding SomeProp}" />
</Border>

<DataTemplate x:Key="TemplateA">
    <ContentPresenter Content="{StaticResource Foo}" />
</DataTemplate>

<DataTemplate x:Key="TemplateB">
    <ContentPresenter Content="{StaticResource Foo}" />
</DataTemplate>

Important: Make sure to use the x:Shared attribute on your shared content or this will not work.

M

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