如何基于默认的DataGrid样式创建样式?

Ras*_*sto 26 c# wpf datagrid styles

我有扩展的自定义控件DataGrid.它被称为ExtendedDataGrid.我希望为它提供样式ExtendedDataGridDataGrids样式相同,除非它更改模板.我尝试过这样的事情:

<Style TargetType="{x:Type MyControls:ExtendedDataGrid}" BasedOn="{StaticResource {x:Type DataGrid}}">
    <Setter Property="Template">
    ...
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

但它说没有找到资源.

所以我尝试:

<Style TargetType="{x:Type MyControls:ExtendedDataGrid}" BasedOn="{StaticResource {ComponentResourceKey ResourceId=DataGridStyle, TypeInTargetAssembly={x:Type DataGrid}}}">
    <Setter Property="Template">
    ...
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

但它也行不通......那我该怎么办?

Ras*_*sto 50

好神秘解决了:)

我上面的第一个代码确实有效:

<Style TargetType="{x:Type MyControls:ExtendedDataGrid}" BasedOn="{StaticResource {x:Type DataGrid}}">
    <Setter Property="Template">
    ...
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

我认为它不起作用因为VS(或Resharper)在我的代码中显示错误,说没有找到资源... VS(或Resharper)中的Bug :(

  • 因为除非他们有一个非常好的借口(不知道如何写XAML不是一个:P),否则任何人都不应该使用它,它常常声称某些东西在确实有效的情况下不起作用. (9认同)