Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create style based on default DataGrid style?

I have custom control that extends DataGrid. It is called ExtendedDataGrid. I want to provide style for ExtendedDataGrid that is the same as DataGrids style except it changes the template. I have tried something like this:

<Style TargetType="{x:Type MyControls:ExtendedDataGrid}" BasedOn="{StaticResource {x:Type DataGrid}}">
    <Setter Property="Template">
    ...
    </Setter>
</Style>

But it says that that the resource is not found.

So I try:

<Style TargetType="{x:Type MyControls:ExtendedDataGrid}" BasedOn="{StaticResource {ComponentResourceKey ResourceId=DataGridStyle, TypeInTargetAssembly={x:Type DataGrid}}}">
    <Setter Property="Template">
    ...
    </Setter>
</Style>

But it also does not work... So what do I do ?

like image 374
Rasto Avatar asked Feb 13 '11 18:02

Rasto


2 Answers

Well mystery is solved :)

My first code above actually works:

<Style TargetType="{x:Type MyControls:ExtendedDataGrid}" BasedOn="{StaticResource {x:Type DataGrid}}">
    <Setter Property="Template">
    ...
    </Setter>
</Style>

I thought that it is not working becase VS (or Resharper) showed error in my code saying that resource is not found... Bug in VS (or Resharper) :(

like image 68
Rasto Avatar answered Nov 17 '22 14:11

Rasto


If you create a style with a TargetType property and base it on another style that also defines a TargetType property, the target type of the derived style must be the same as or be derived from the type of the base style.

Your grid does inherit from DataGrid, right?

like image 27
H.B. Avatar answered Nov 17 '22 12:11

H.B.