4

我有一个DataTemplate在我的 WPF 应用程序中使用的 -

<DataTemplate x:Key="mattersTemplate">
    <Border Name="border" BorderBrush="Aqua" BorderThickness="1" Padding="5" Margin="5">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Row="0" Grid.Column="0" Text="FileRef:"/>
            <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=FileRef}" />
            <TextBlock Grid.Row="1" Grid.Column="0" Text="Description:"/>
            <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=Description}"/>
            <TextBlock Grid.Row="2" Grid.Column="0" Text="Priority:"/>
            <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Path=Priority}"/>
        </Grid>
    </Border>
</DataTemplate>

然后我(在一个DocumentSetTemplateSelector类中)定义要使用的模板;

我想做/知道的是;创建 4 个其他模板,这些模板将继承上述模板,然后允许覆盖某些属性;

一个例子(这个模板继承自上面的类)——所以它们看起来是一样的;

<DataTemplate x:Key="documentSet_Accounting">
    <ContentPresenter Content="{Binding mattersTemplate}" 
         ContentTemplate="{StaticResource mattersTemplate}">
    </ContentPresenter>
</DataTemplate>

我希望有一种风格附加到这个(如果可能的话)以便获得这种效果;

<DataTemplate x:Key="documentSet_Accounting">
    <ContentPresenter fontsize="20" Content="{Binding mattersTemplate}" 
         ContentTemplate="{StaticResource mattersTemplate}">
    </ContentPresenter>
</DataTemplate>

或者

<DataTemplate x:Key="documentSet_Accounting">
    <ContentPresenter Style="AccountingStyle" Content="{Binding mattersTemplate}" 
         ContentTemplate="{StaticResource mattersTemplate}">
    </ContentPresenter>
</DataTemplate>
4

1 回答 1

1

在模板中使用样式继承而不是模板继承怎么样?

<Style x:Key="mattersTemplateStyle">
    <Setter Property="TextBlock.Foreground" Value="Green"/>
</Style>
<Style x:Key="documentSet_AccountingStyle" BasedOn="{StaticResource mattersTemplateStyle}">
    <Setter Property="TextBlock.FontSize" Value="20"/>            
</Style>
<DataTemplate x:Key="mattersTemplate">
    <Border Name="border" BorderBrush="Aqua" BorderThickness="1" Padding="5" Margin="5">
        <Grid Style="{StaticResource mattersTemplateStyle}">
            [...]
        </Grid>
    </Border>
</DataTemplate>
<DataTemplate x:Key="documentSet_Accounting">
    <Grid Style="{StaticResource documentSet_AccountingStyle}">
        <ContentPresenter Content="{Binding mattersTemplate}" ContentTemplate="{StaticResource mattersTemplate}"></ContentPresenter>
    </Grid>
</DataTemplate>
于 2012-11-28T12:37:34.393 回答