如何应用一种样式来控制另一种样式的模板?

时间:2011-12-22 14:10:36

标签: wpf xaml styles

我有控制风格:

<Style x:Key="base style" TargetType="{x:Type cust:SomeCustomControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type cust:SomeCustomControl}">
                <DataGrid >
                    <!-- some content... -->
                </DataGrid >
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我尝试添加另一个样式,它应该与之前的样式完全相同,但DataGrid中的第一行使用“Bold”字体:

<Style x:Key="bold row" TargetType="{x:Type cust:SomeCustomControl}" BasedOn="{StaticResource base style}">
??????????????
??????????????
</Style>

但我不明白如何在不复制“基本风格”的整个代码的情况下更改第一种风格的某些属性。 我想我应该添加如下内容:

<Style TargetType="{x:Type DataGridCell}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding RowIndex}" Value="0">
            <Setter Property="FontWeight" Value="Bold"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

但我的“粗体行”风格适用于cust:SomeCustomControl。那么如何在“粗体行”风格中做到这一点,而不会覆盖整个

<Setter Property="Template">

1 个答案:

答案 0 :(得分:0)

难道你不能只将DataGridCell样式添加到inheirted样式的资源中吗?

<Style x:Key="bold row" TargetType="{x:Type cust:SomeCustomControl}" BasedOn="{StaticResource base style}">
    <Style.Resources>
        <Style TargetType="{x:Type DataGridCell}">
            <Style.Triggers>
               <DataTrigger Binding="{Binding RowIndex}" Value="0">
                    <Setter Property="FontWeight" Value="Bold"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Style.Resources>
</Style>

它应该使用相同的模板,但是继承的模板会将触发器应用于所有DataGridCell个对象