2

ContentControl で使用するために、windows.resources で複数の DataTemplate を指定するにはどうすればよいですか? 私のコード:

<Window.Resources>
    <DataTemplate x:Key="CustomerTemplate" DataType="{x:Type local:Customer}">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding FirstName}"/>
            <TextBlock Text=" ("/>
            <TextBlock Text="{Binding Occupation}"/>
            <TextBlock Text=")"/>
        </StackPanel>
    </DataTemplate>
    <DataTemplate x:Key="PersonTemplate" DataType="{x:Type local:Person}">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding FirstName}"/>
            <TextBlock Text=" - "/>
            <TextBlock Text="{Binding LastName}"/>
        </StackPanel>
    </DataTemplate>
</Window.Resources>

どうもありがとう!

4

2 に答える 2

1

DataTemplateSelector を使用して、適用する Datatemplate を返します。

<ContentControl ContentTemplateSelector="{StaticResource MyTemplateSelector}"/>

ここで MYtemplateselector はDataTemplateSelector、セレクターの Select() メソッドで contentcontrol にバインドされたプロパティをチェックし、対応する Datatemplate を返すことができます。

ありがとう

于 2013-09-05T09:17:08.827 に答える
0

から削除x:KeyしてDataTemplate、これを試してください:

<ContentControl Name="CustomerContentControl">
    <local:Customer />
</ContentControl>

<ContentControl Name="PersonContentControl">
    <local:Person />
</ContentControl>

この記事では、次Josh Smithの要素にアクセスする方法を示しますDataTemplate

ContentControl で FindName を使用する方法

于 2013-09-05T09:18:44.037 に答える