Tzwenni
Tzwenni

Reputation: 139

DataGridTemplateColumn.CellTemplate is not working when ContentPresenter.ContentTemplate DataTemplate is modified

I do have following style:

<Style x:Key="DefaultDataGridCell"  TargetType="DataGridCell">
    <Setter Property="FontSize" Value="13"/>
    <Setter Property="Margin" Value="0" />
    <Setter Property="Foreground" Value="Black"/>
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Border x:Name="CellBorder" Padding="1.5,0" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" SnapsToDevicePixels="True">
                    <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
                        <!--Here my problems of understanding are starting-->
                        <ContentPresenter.ContentTemplate>
                            <DataTemplate >
                                <TextBlock Text="{Binding Text}" 
                                           Padding="2,0" 
                                           Background="{Binding Background}"
                                           ToolTip="{Binding ToolTip}"
                                           />
                            </DataTemplate>
                        </ContentPresenter.ContentTemplate>
                    </ContentPresenter>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

With above Style this XAML doesn't work as I hoped:

<DataGridTemplateColumn Header="Eskalation" >
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="15"/>
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" >
                    <TextBlock.Background>
                        <MultiBinding ConverterParameter="EskaStufe1" Converter="{StaticResource EskalationsBackgroundConverter}">
                            <Binding Path="Status"/>
                            <Binding Path="Prioritat"/>
                            <Binding Path="ErfasstAm"/>
                        </MultiBinding>
                    </TextBlock.Background>
                </TextBlock>
            </Grid>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

The <DataGridTemplateColumn.CellTemplate> is ignored. I was hoping this XAML would get higher priority.

If I delete the <ContentPresenter.ContentTemplate> tag, everything fine. This works then as I expect.

<Style x:Key="DefaultDataGridCell"  TargetType="DataGridCell">
    <Setter Property="FontSize" Value="13"/>
    <Setter Property="Margin" Value="0" />
    <Setter Property="Foreground" Value="Black"/>
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Border x:Name="CellBorder" Padding="1.5,0" Background="{TemplateBinding Background}" 
                        BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" 
                        SnapsToDevicePixels="True">
                    <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
               </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

So I need help to understand how I should change my XAML to get this working.

Thanks a lot !

Upvotes: 0

Views: 613

Answers (1)

Tzwenni
Tzwenni

Reputation: 139

After a few hours of sleep, I came up with the solution. Got up quietly and propped up straight away.

So easy. Defined a new style and applied it to the affected column.

<Style x:Key="EskalationsDataGridCellStyle"  TargetType="DataGridCell" 
        BasedOn="{StaticResource DefaultDataGridCell}">
    <Setter Property="Margin" Value="2,0" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Border x:Name="CellBorder" Padding="1.5,0" Background="{TemplateBinding Background}" 
                        BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" SnapsToDevicePixels="True">
                    <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

And the using:

<DataGridTemplateColumn Header="Eskalation" CellStyle="{StaticResource EskalationsDataGridCellStyle}" >

Upvotes: 1

Related Questions