2011-02-13 112 views
22

我有擴展DataGrid的自定義控件。它被稱爲ExtendedDataGrid。我想爲ExtendedDataGrid提供與DataGrid s樣式相同的樣式,但它會更改模板。我試過這樣的事情:如何基於默認的DataGrid風格創建樣式?

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

但它說沒有找到資源。

所以我嘗試:

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

但它也不起作用......所以,我該怎麼辦?

回答

44

好謎團解開:)

我上面居然第一代碼工作:

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

我認爲這是不工作becase的VS(或ReSharper的)在我的代碼顯示錯誤,指出資源沒有找到...在VS(或Resharper)中的錯誤:(

2

如果創建一個風格與 TargetType的屬性和它的基礎上 另一個風格,也定義了 TargetType的屬性,目標類型派生的風格的 必須相同 的或從派生基本樣式的 類型。

您的網格確實從DataGrid繼承,對嗎?

+0

是的,它的確解決了問題,請參閱我上面的回答:) – drasto 2011-02-13 18:27:41

+0

想知道如果不是這樣,那麼因爲你注意到它可以工作。 – 2011-02-13 18:30:52