触发内容模板 [英] Triggered Content Template
问题描述
我想设置一个变化基于一个DataTrigger一个的ContentTemplate。 Syntatically,我觉得这应该工作,但它会导致堆栈溢出试图渲染页面时:
I'm trying to set up a ContentTemplate that changes based on a DataTrigger. Syntatically, I feel like this should work, but it results in a stack overflow when trying to render the page:
<ItemsControl ItemsSource="{Binding Path=ExtendedFieldCollection}" ItemTemplate="{StaticResource RequiredFieldsTemplate}" />
<!--Where-->
<DataTemplate x:Key="RequiredFieldsTemplate">
<ContentPresenter>
<ContentPresenter.Style>
<Style TargetType="ContentPresenter">
<Setter Property="ContentTemplate" Value="{x:Null}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsRequired}" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource MyFieldDisplayTemplate}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentPresenter.Style>
</ContentPresenter>
</DataTemplate>
只需要去
Simply going
<DataTemplate x:Key="RequiredFieldsTemplate">
<ContentPresenter ContentTemplate="{StaticResource MyFieldDisplayTemplate}" />
精品工程 - 但我想要做到的是绑定到字段列表,但只显示所需的字段。我不能只用能见度=崩溃,有在 MyFieldDisplayTemplate
实例的所有自定义控件巨大的开销。我的目标是让非必填字段有一个完全不同的(空)控制ItemsControl中的模板。
Works fine - but what I'm trying to accomplish is to bind to a list of fields, but only display the required fields. I can't just use Visibility=collapsed, there is huge overhead in instantiating all the custom controls in the MyFieldDisplayTemplate
. My goal is to have non-required fields have a completely different (empty) control template in the ItemsControl.
这是如何设置这个触发任何想法?
Any ideas on how to set up this trigger?
推荐答案
我理解了它感谢在这个问题绊脚石:的 WPF:如何设置数据模板触发内容控制
I figured it out thanks to stumbling upon this question: WPF: How to set the data template trigger for content control?
我应该使用一个ContentControl中 - 不是一个ContentPresenter。奇怪,这两个行为相同的,当你使用它们的一种方式,但完全不同,当你想使用触发器。 。多少一如既往学习
I should be using a ContentControl - not a ContentPresenter. Strange, the two behave the same when you use them one way, but completely differently when you want to use triggers. Much to learn as always.
<DataTemplate x:Key="RequiredFieldsTemplate">
<ContentControl>
<ContentControl.Style>
<Style TargetType="ContentPresenter">
<Setter Property="ContentTemplate" Value="{x:Null}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsRequired}" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource MyFieldDisplayTemplate}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</DataTemplate>
这篇关于触发内容模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!