首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >复DataTemplate

复DataTemplate
EN

Stack Overflow用户
提问于 2012-09-20 07:57:47
回答 1查看 188关注 0票数 1

我实现了CustomDataTemplateSelector,如下所示:Windows 7 DataTemplateSelector和CustomDataTemplateSelector的实现。但在我的解决方案中,只有一个部分通过所有DataTemplates进行更改,DataTemplates的其他部分是常见的:

代码语言:javascript
复制
<local:MyTemplateSelector Content="{Binding}">
    <local:MyTemplateSelector.OneTemplate>
        <DataTemplate>
            <Grid Orientation="Horizontal" >
                <Grid x:Name="Grid1">
                    <Image Height="60" Width="60" Source="{Binding Photo}"/>
                </Grid>
                <Grid>
                    <TextBlock Text="{Binding TextValue1}">
                    <TextBlock Text="{Binding TextValue2}">
                </Grid>
            </Grid>
        </DataTemplate>
    </local:MyTemplateSelector.OneTemplate>
    <local:MyTemplateSelector.AnotherTemplate>
        <DataTemplate>
            <Grid Orientation="Horizontal" >
                <Grid x:Name="Grid2">
                    <Image Height="30" Width="60" Source="{Binding Photos[0]}"/>
                    <Image Height="30" Width="60" Source="{Binding Photos[1]}"/>
                </Grid>
                <Grid>
                    <TextBlock Text="{Binding TextValue1}">
                    <TextBlock Text="{Binding TextValue2}">
                </Grid>
            </Grid>
        </DataTemplate>
    </local:MyTemplateSelector.AnotherTemplate>
</local:MyTemplateSelector>

在这里,Grid1和Grid2是不同的部分。“拆分”这些DataTemplates是可能的吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-20 08:20:35

尝试将公共部分声明为资源,并将其绑定到ContentPresenter:

代码语言:javascript
复制
<DataTemplate x:Key="CommonPart">
   <Grid >
      <TextBlock Text="{Binding TextValue1}">
      <TextBlock Text="{Binding TextValue2}">
   </Grid>
</DataTemplate>

<local:MyTemplateSelector Content="{Binding}">
<local:MyTemplateSelector.OneTemplate>
<DataTemplate>
   <Grid Orientation="Horizontal" >
      <Grid x:Name="Grid1">
         <Image Height="60" Width="60" Source="{Binding Photo}"/>
      </Grid>
      <ContentPresenter ContentTemplate="{StaticResource CommonPart}" />                
   </Grid>
</DataTemplate>
</local:MyTemplateSelector.OneTemplate>
<local:MyTemplateSelector.AnotherTemplate>
<DataTemplate>
   <Grid Orientation="Horizontal" >
      <Grid x:Name="Grid2">
         <Image Height="30" Width="60" Source="{Binding Photos[0]}"/>
         <Image Height="30" Width="60" Source="{Binding Photos[1]}"/>
      </Grid>
      <ContentPresenter ContentTemplate="{StaticResource CommonPart}" /> 
   </Grid>
</DataTemplate>
</local:MyTemplateSelector.AnotherTemplate>
</local:MyTemplateSelector>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12508303

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档