Gerard
Gerard

Reputation: 13397

Style inside ContentPresenter is not applied

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>

Upvotes: 4

Views: 1852

Answers (1)

Reza ArabQaeni
Reza ArabQaeni

Reputation: 4907

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.

Upvotes: 4

Related Questions