<Window x:Class="Using_Inherited_Dps.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowStartupLocation="CenterScreen" 
Title="依赖项属性的继承" Height="400" Width="578" Foreground="Red">
    <StackPanel >
        <Label Content="熊俊" />
    </StackPanel>
</Window>
上面的Lable的字体没有继承Window的Red,不是红色,为什么呢?

解决方案 »

  1.   


    <Style x:Key="LabelStyle1"
            TargetType="{x:Type Label}">
        <Setter Property="Foreground"
                Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
        <Setter Property="Background"
                Value="Transparent" />
        <Setter Property="Padding"
                Value="5" />
        <Setter Property="HorizontalContentAlignment"
                Value="Left" />
        <Setter Property="VerticalContentAlignment"
                Value="Top" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Label}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Background="{TemplateBinding Background}"
                            Padding="{TemplateBinding Padding}"
                            SnapsToDevicePixels="True">
                        <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}"
                                            Content="{TemplateBinding Content}"
                                            ContentStringFormat="{TemplateBinding ContentStringFormat}"
                                            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                            RecognizesAccessKey="True"
                                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled"
                                    Value="False">
                            <Setter Property="Foreground"
                                    Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    DynamicResource {x:Static SystemColors.ControlTextBrushKey}
      

  2.   

    <Window x:Class="WpfApplication2.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525" Foreground="Red">
        <StackPanel >
            <Label Content="熊俊" />    <TextBlock Text="12334" />
        </StackPanel></Window>TextBlock 是有效果的哦MSDN上是这么写的
    This property only affects a control whose template uses the Foreground property as a parameter. On other controls, this property has no impact.
    所以对TextBlock可以起作用而CheckBox, Button一类的就不起作用。我们在使用Button的Foreground属性时,实际上也只是对Button内部的TextBlock的Foreground产生了作用,用Snoop查看Button的VisualTree可以发现
    Button
      Chrome
        ContentPresenter
          TextBlock也就是Button的内容(字符串)实际上是在TextBlock中被显示出来,并且Foreground属性也是被应用到了TextBlock上。所以LZ还是一个一个设置吧~~~
      

  3.   

    如果你想用的话 TextBlock  也有lable的效果
      

  4.   

    我复制Lable的模板,并删除了Foreground属性的Setter,然后,把Window的Foreground属性设置为Red,结果Label还是没有继承Foreground...........
      

  5.   

    我复制Lable的模板,并删除了Foreground属性的Setter,然后,把Window的Foreground属性设置为Red,结果Label还是没有继承Foreground........... 
      

  6.   

    我复制Lable的模板,并删除了Foreground属性的Setter,然后,把Window的Foreground属性设置为Red,结果Label还是没有继承Foreground...........  
      

  7.   

    我复制Lable的模板,并删除了Foreground属性的Setter,然后,把Window的Foreground属性设置为Red,结果Label还是没有继承Foreground...........   有人知道是什么原因吗? ?............ 
      

  8.   

    <TextBlock.Foreground>  
                        <LinearGradientBrush>   
                                <GradientStop Color="Green"></GradientStop>  
                                <GradientStop x:Name="gcc1"  Color="Green" Offset="0.3"></GradientStop>  
                                <GradientStop x:Name="gcc2" Color="Blue" Offset="0.3"></GradientStop>  
                                <GradientStop Color="Blue" Offset="1"></GradientStop>   
                        </LinearGradientBrush>  
                    </TextBlock.Foreground>  
    你看看这段代码,希望对你有帮助,这是设置的TextBlock的属性颜色,改成Lable就行了
      

  9.   

     <StackPanel > 
            <Label Content="熊俊" Foreground="Red" /> 
        </StackPanel>