4

I have the following xaml:

<DataGrid ItemsSource="{Binding}" AutoGenerateColumns="True">
    <DataGrid.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="DataGridCell">
                        <Grid>
                            <ContentPresenter Height="50">
                                <ContentPresenter.Resources>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="Padding" Value="4"/>
                                    </Style>
                                </ContentPresenter.Resources>
                            </ContentPresenter>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </DataGrid.CellStyle>
</DataGrid>

It turns out that the TextBlock has a default padding of 2,0.
Why is the style not applied?

EDIT: I used this solution (from here) that takes the text from the autogenerated TextBlock (Content.Text) and displays it in another TextBlock:

<ControlTemplate TargetType="{x:Type DataGridCell}">
    <Grid SnapsToDevicePixels="True">
        <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, 
                                  Path=Content.Text}" Padding="4"/>
    </Grid>
</ControlTemplate>
Community
  • 1
  • 1
Gerard
  • 13,023
  • 14
  • 72
  • 125
  • 1
    Why not put an actual `TextBlock` inside your cell template, with its `Padding` set to 4? – Pieter Witvoet Dec 17 '14 at 10:06
  • Your idea renders a Textblock with a ContainerViusal inside that in turn contains a ContentPresenter: somewhat unusual. Also the idea only works when I use Margin instead of Padding. – Gerard Dec 17 '14 at 11:06

1 Answers1

4

Implicit styles are applied to elements in templates if the element inherits from Control, and TextBlock not inherited from Control.

To more information read this article: MSDN Blog

Update Answer

By point that @JustinXL menthioned and after i snoop DataGrid, In this scope template shouldn't lost style and i found a local style did set to TextBlock(Generated by DataGrid) that override implicit style.

Reza ArabQaeni
  • 4,848
  • 27
  • 46
  • This is not correct. TextBlock can be styled with implicit styles. – Justin XL Dec 17 '14 at 10:45
  • @JustinXL, as i mentioned not in datatemplates.This behavior is by design. – Reza ArabQaeni Dec 17 '14 at 10:48
  • Also this answer that i found say this point: http://stackoverflow.com/a/2479695/440030 – Reza ArabQaeni Dec 17 '14 at 10:50
  • I know, but his case is different. He defines the implicit style inside the `ContentPresenter` which makes the `TextBlock` and the implicit style in the same scope. I can only guess that there's something else (another named `TextBlock` style) that overrides the implicit style. – Justin XL Dec 17 '14 at 10:58
  • 1
    @JustinXL, You right, In this scope template shouldn't lost style. I snoop DataGrid and i found a local style set to TextBlock(Generated by DataGrid) that override implicit style. Thanks for your attention. – Reza ArabQaeni Dec 17 '14 at 11:29
  • Your welcome. :) And that's a good find, maybe update your answer with it? – Justin XL Dec 17 '14 at 11:33