1

I have a question about automatic style inheritance.

Basically, I have a superclass for which I would like to define the default style for textboxes and textblocks for classes that derive from it. These styles are based on my default styles as defined in 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>

This class is called DataEntryView.

Now when I derive from this class, the textblock and textboxes just get their default Windows look, not what I defined here.

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

What is it that I'm forgetting?

Basically, I don't want to explicitly set the Style of every textblock and textbox to some key.

4

1 回答 1

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>
于 2012-11-21T08:55:25.367 回答