Davut Gürbüz
Davut Gürbüz

Reputation: 5716

Giving a specific style to DataGrid element (implicitly)

I'm trying implicitly apply a style for DataGrid and TextBlocks.

For TextBlock's ForeGround I need White color.

For DataGrid's rows I need Black Color.

Beside this I need White again for DataGrid's header columns.

enter image description here

When I globally apply an implicit style for on MainPage by

<UserControl>
 <UserControl.Resorces>
    <Style targetType="TextBlock">
        <Setter Property="Foreground" Value="White"/>
    </Style>
 </UserControl.Resorces>
</UserControl>

Making TextBlock's Foreground White operation is done! But beside this all of elements in DataGrid (By default content elements are textblock's I think) turn to White color.

It doesn't look good White on white as you guess :) So how can I particularly specify DataGrid's elements Foreground to black?

I can do it by using same technic shown below ,but this is an expensive operation for each DataGrid. As a con more I want DataGrid's HeaderColumns white again.This operation make them all black.

Is there an explicit way such as we do in css styles?

Here is what I tried to achieve this goal by Control template. But no chance because of being DataGrid's ContentControl is dynamic.

<DataGrid>
<DataGrid.Resources>
    <Style targetType="TextBlock">
        <Setter Property="Foreground" Value="Black"/>
    </Style>
<DataGrid.Resources>

In fact we use Telerik's RadGridView but I give a sdk's DataGrid example to make question more global.

<Style TargetType="sdk:DataGrid">
    <Setter Property="Foreground" Value="Black"/>
    <Setter Property="RowDetailsTemplate" Value="{StaticResource DataTemplate1}"/>
    <Setter Property="Template" Value="{StaticResource ControlTemplate1}"/>
</Style>

<ControlTemplate x:Key="ControlTemplate1" TargetType="sdk:DataGrid">
    <Grid/>
</ControlTemplate>

<DataTemplate x:Key="DataTemplate1">
    <Grid/>
</DataTemplate>

Thanks in advance!

Upvotes: 0

Views: 168

Answers (1)

Chris W.
Chris W.

Reputation: 23280

If it were me I would pull out the full control templates and style them accordingly instead of trying to just do adhoc setter changes to override bits of the original template. In Expression Blend right click, choose "Edit Template -> Edit A Copy" and break out the templates for your rows etc and apply those implicitly with StaticResource instead.

Upvotes: 1

Related Questions