2012-11-21 79 views
1

我有一個關於自動樣式繼承的問題。自動樣式繼承

基本上,我有一個超類,我想爲它派生的類定義文本框和文本塊的默認樣式。這些樣式基於Styles.xaml中定義的我的默認樣式。

<controls:BaseUserControl.Resources> 

    <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource DisplayLabel}"> 
     <Setter Property="Foreground" Value="Black"/> 
     <Setter Property="Margin" Value="10,0,10,0"/> 
     <Setter Property="HorizontalAlignment" Value="Left"/> 
     <Setter Property="VerticalAlignment" Value="Center"/> 
    </Style> 

    <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource InputField}"> 
     <Setter Property="Height" Value="30"/> 
     <Setter Property="Margin" Value="10,0,0,0"></Setter> 
     <Setter Property="HorizontalAlignment" Value="Left"/> 
     <Setter Property="VerticalAlignment" Value="Center"/> 
     <Setter Property="VerticalContentAlignment" Value="Center"></Setter> 
    </Style> 

</controls:BaseUserControl.Resources> 

本課程稱爲DataEntryView

現在,當我從這個類派生出來的時候,textblock和textboxes只是獲得了他們默認的Windows外觀,而不是我在這裏定義的。

<views:DataEntryView.Resources> 
    <!-- Do I need to add anything here? --> 
</views:DataEntryView.Resources> 

什麼是我忘了?

基本上,我不想明確地將每個文本塊和文本框的樣式設置爲某個鍵。

回答

0

您需要將樣式放在RessourceDictionary中,並使用RessourceDictionary.MergedDictionaries屬性將其包含到超類和派生類中。

樣式文件(Styles.xaml)

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" ....> 
    <Style ...> <!--Your styles goes here--> 
</ResourceDictionary> 

控制(父母和子女):

<UserControl.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="Styles.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</UserControl.Resources>