2012-11-21 77 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>