2012-10-22 110 views
44

如何在Silverlight中創建基於默認樣式的樣式?如何基於默認樣式創建樣式?

例如,在WPF我們使它像:

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}"> 
    <Setter Property="Margin" Value="2" /> 
    <Setter Property="Padding" Value="2" /> 
</Style> 
+5

我來到這個問題尋找如何在WPF中做到這一點的答案:) –

回答

18

幾乎相同。只需減去x:Type更明確的命名。

<Style TargetType="TextBox" BasedOn="{StaticResource DefaultTextBoxStyle}"> 

更多信息here in the docs。 PS,如果你需要的默認模板,文本框例如通常會在CoreStyles.xaml發現在你的答案的第一讀混淆的情況下,該意見要求

附錄; 「你需要一個基礎樣式,這很容易做到,因爲你打算在創建像ToolkitStyles.xaml,SDKStyles.xaml,CoreStyles.xaml等文件的應用程序主題中執行該操作。 。這也正是在回答名稱由」

+2

但如果我想繼承自定義控件的默認樣式(開發 由我)? – ZuTa

+1

也許我的問題並不明顯.. – ZuTa

+0

哦,那麼只需用自己的領域替換領域。像使用YourStyleTemplate代替DefaultTextBoxStyle,等等。對吧? –

11

來到基於默認樣式,創建樣式,您需要創建一個命名樣式,然後根據指定的樣式默認樣式(http://weblogs.asp.net/lduveau/silverlight-how-to-inherit-from-an-implicit-style

<Style x:Key="DefaultCustomControlStyle" TargetType="local:CustomControl"> 
    <Setter Property="Padding" Value="2" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="local:CustomControl"> 
       <ContentPresenter /> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
<Style TargetType="local:CustomControl" BasedOn="{StaticResource DefaultCustomControlStyle}" />