0

我正在尝试基于 Binding 值应用 ContentTemplate。问题是,它不起作用。

我有一个名为 TemplateA 的默认模板,然后我想显示基于数据绑定值的样式 - TemplateA 或 TemplateB。

如果我注释掉默认模板,则不会选择任何模板。

我检查了我的数据绑定值,该值没问题。

你能看出我哪里错了吗?

这是 ListDataView

<CollectionViewSource x:Key="ListDataView" />

它位于 Window 的 Resources 部分,ListDataView 附加到代码中的 ObservableCollection。

<DataTemplate x:Key="TemplateA">
    <TextBlock Text="Template A" />
</DataTemplate>

<DataTemplate x:Key="TemplateB">
    <TextBlock Text="Template B" />
</DataTemplate>

    <ContentControl x:Name="LISTINGCONTROLA">
    <ContentControl.Style>
        <Style TargetType="{x:Type ContentControl}">
            <Setter Property="ContentTemplate" Value="{StaticResource TemplateA}" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Source={StaticResource ListDataView}, Path=ListType}" Value="TEMPLATEA">
                    <Setter Property="ContentTemplate" Value="{StaticResource TemplateA}" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Source={StaticResource ListDataView}, Path=ListType}" Value="TEMPLATEB">
                    <Setter Property="ContentTemplate" Value="{StaticResource TemplateB}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ContentControl.Style>
</ContentControl>
4

2 回答 2

1

使用 WPF DataTemplateSelector可能会有所帮助 http://www.switchonthecode.com/tutorials/wpf-tutorial-how-to-use-a-datatemplateselector

于 2011-10-17T13:01:52.877 回答
1

问题很可能是因为您的绑定指向CollectionViewSource自身的属性,并且该类没有命名的属性ListType,因此没有要绑定的值(因此始终使用默认值)。

ListType您要绑定的属性在哪里?

于 2011-10-17T13:48:44.453 回答