user2970725
user2970725

Reputation:

How do you change the foreground color of a stackpanel in xaml

I have this in my App.xaml

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

And this on a xaml page

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

In the designer the color changes to black but when i run the application on a device it crashes and says it doesn't know the name ColorBlack.

Upvotes: 1

Views: 3642

Answers (1)

Christopher Sauer
Christopher Sauer

Reputation: 136

You can try something like this :

<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>

Upvotes: 2

Related Questions