如何在xaml中更改stackpanel的前景色

如何在xaml中更改stackpanel的前景色,xaml,Xaml,我的App.xaml中有这个 <Style x:Key="ColorBlack" TargetType="StackPanel"> <Setter Property="TextElement.Foreground" Value="Black"></Setter> </Style> 这是一个xaml页面 <StackPanel Style="{StaticResource ColorBlack}"></Sta

我的App.xaml中有这个

 <Style x:Key="ColorBlack" TargetType="StackPanel">
      <Setter Property="TextElement.Foreground" Value="Black"></Setter>
 </Style>

这是一个xaml页面

 <StackPanel Style="{StaticResource ColorBlack}"></StackPanel>


在designer中,颜色变为黑色,但当我在设备上运行应用程序时,它会崩溃,并说它不知道ColorBlack的名称。

您可以尝试以下方法:

<StackPanel>
    <StackPanel.Resources>
        <Style TargetType="{x:Type FrameworkElement}">
            <Setter Property="TextBlock.Foreground" Value="Blue" />
        </Style>
        <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource {x:Type TextBlock}}" />
        <Style TargetType="{x:Type Label}" BasedOn="{StaticResource {x:Type Label}}" />
        <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type FrameworkElement}}" />
        <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type TextBox}}" />

    </StackPanel.Resources>
    <TextBlock Text="TextBlock"/>
    <Label Content="Label"/>
    <Button Content="Button"/>
    <TextBox Text="TextBox"/>
</StackPanel>


我可能错了,但是StackPanel没有前台属性。。。它是一个用于保存其他UI组件的元素,本身没有视觉效果。要将隐式样式应用于元素,TargetType必须完全匹配。它不适用于像FrameworkElement这样的基类。