在 WPF 中,您可以像使用样式一样将一个 DataTemplate 建立在另一个 DataTemplate 之上吗?
可能的重复:
数据模板继承
我有几种不是子类的数据类型,它们也不共享接口,但它们确实有共同点我想要在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@Foovanadil,实际上我想我想出了更好的东西。我的新方法不仅避免了额外的绑定(内容呈现器上的绑定),而且还消除了在显式设置其内容时由该呈现器应用模板的需要。这两件事都应该可以加快你的 UI 速度,尤其是在更大、更复杂的界面中。
重要提示:请确保在共享内容上使用
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.
Important: Make sure to use the
x:Shared
attribute on your shared content or this will not work.M