I have custom control that extends DataGrid
. It is called ExtendedDataGrid
. I want to provide style for ExtendedDataGrid
that is the same as DataGrid
s style except it changes the template. I have tried something like this:
我有扩展DataGrid的自定义控件。它被称为ExtendedDataGrid。我想为ExtendedDataGrid提供与datagrid样式相同的样式,但它更改了模板。我试过以下方法:
<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 ?
但它也不起作用……我该怎么做?
2 个解决方案
#1
45
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) :(
我认为它不能工作,因为VS(或Resharper)在我的代码中显示错误,说找不到资源……错误在VS(或Resharper):(
#2
2
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.
如果您使用TargetType属性创建一个样式,并将其基于另一个样式(也定义了TargetType属性),则派生样式的目标类型必须与基本样式的类型相同或派生。
Your grid does inherit from DataGrid, right?
你的网格确实继承自DataGrid,对吧?
#1
45
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) :(
我认为它不能工作,因为VS(或Resharper)在我的代码中显示错误,说找不到资源……错误在VS(或Resharper):(
#2
2
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.
如果您使用TargetType属性创建一个样式,并将其基于另一个样式(也定义了TargetType属性),则派生样式的目标类型必须与基本样式的类型相同或派生。
Your grid does inherit from DataGrid, right?
你的网格确实继承自DataGrid,对吧?