Triggered Content Template

喜夏-厌秋 提交于 2019-12-06 01:52:26
Alain

I figured it out thanks to stumbling upon this question: WPF: How to set the data template trigger for content control?

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!