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 に答える