【问题标题】:Automatic Style Inheritance自动样式继承
【发布时间】:2012-11-21 08:37:50
【问题描述】:

我有一个关于自动样式继承的问题。

基本上,我有一个超类,我想为它定义派生自它的类的文本框和文本块的默认样式。这些样式基于我在 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。

现在,当我从此类派生时,文本块和文本框只是获得默认的 Windows 外观,而不是我在此处定义的外观。

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

我忘记了什么?

基本上,我不想将每个文本块和文本框的样式显式设置为某个键。

【问题讨论】:

    标签: wpf


    【解决方案1】:

    您需要将样式放在 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-24
      • 2011-03-16
      • 2016-12-14
      相关资源
      最近更新 更多