Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 自动样式继承_Wpf - Fatal编程技术网

Wpf 自动样式继承

Wpf 自动样式继承,wpf,Wpf,我有一个关于自动样式继承的问题 基本上,我有一个超类,我想为它定义文本框的默认样式,并为从它派生的类定义文本块。这些样式基于styles.xaml中定义的默认样式 <controls:BaseUserControl.Resources> <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource DisplayLabel}"> <Setter Property="Foreg

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

基本上,我有一个超类,我想为它定义文本框的默认样式,并为从它派生的类定义文本块。这些样式基于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>
我忘记了什么


基本上,我不想显式地将每个textblock和textbox的样式设置为某个键。

您需要将样式放置在ResourceDictionary中,并使用ResourceDictionary.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>
<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Styles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>