从样式

时间:2016-02-17 09:15:23

标签: c# wpf styles controltemplate

我希望在StyleBorder ControlTemplate。但我不知道如何访问它。我的Style看起来像这样:

<Style x:Key="RedCell" TargetType="DataGridCell" BasedOn="{StaticResource MYDGCellStyle}">            
        <Setter Property="Foreground" Value="White" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="DataGridCell">
                    <Grid>
                        <Grid Grid.ZIndex="86" x:Name="CellContenGrind" Background="{TemplateBinding Background}" />                            
                        <Border Grid.ZIndex="87" x:Name="ContentBorder" BorderBrush="White" Background="Crimson" CornerRadius="25">
                            <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="Center" VerticalAlignment="Center" />
                        </Border>                            
                        <Grid Grid.ZIndex="88"  x:Name="CellGridFocused"/>
                    </Grid>
                    <ControlTemplate.Triggers>

                        <Trigger Property="IsFocused" Value="True">
                            <Setter Property="Background" TargetName="CellGridFocused" Value="{DynamicResource Brush_DataGridCellFocused}" />
                        </Trigger>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" TargetName="CellContenGrind" Value="{DynamicResource Brush_DataGridSelected}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <DataTrigger Binding="{Binding ungelesen}" Value="0">
                <Setter Property="Background" Value="Green"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

由于其他一些内容,我需要{em> CellContentGrind 上的Background="{TemplateBinding Background}",所以我无法将其移至我的Border

如果 ungelesen = 0,我希望 ContentBorder Background为绿色。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

为什么不将Style移到边框本身?

<Border Grid.ZIndex="87"x:Name="ContentBorder" BorderBrush="White" CornerRadius="25">
    <Border.Style>
        <Style TargetType="Border">
            <Setter Property="Background" Value="Crimson" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding ungelesen}" Value="0">
                    <Setter Property="Background" Value="Green"/>
                </DataTrigger>
            </Style.Triggers>                 
        </Style>
    </Border.Style>

    <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border> 

请注意,现在使用Setter设置Background可以使用DataTrigger覆盖。