locked
Give a StackPanel all TextBlock elements the color black

    Question

  • I put in App.xaml

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

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

    In the designer the text becomes black but when i run it on my device i get this error:

    XamlParseException occured

    Cannot find a Resource with the Name/Key ColorBlack

    Sunday, March 16, 2014 8:23 PM

Answers

  • Hi,

    You cannot apply this style to StackPanel. Because the TextElement.Foreground is not the Stackpanel's dependency property. You can refer to the link below:

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.style.aspx

    The link has a note like:

    In order to apply a Style, the target object must be a DependencyObject. The property that each Setter references as a Property value must be a dependency property. 

    If you want to give the TextBlock elements the color,you can define in App.XAML like below:

    <Application.Resources>
            <Style x:Key="ColorBlack" TargetType="TextBlock">
                <Setter Property="Foreground" Value="Blue"></Setter>
            </Style>
        </Application.Resources>
    <StackPanel >
                <TextBlock Text="element" Style="{StaticResource ColorBlack}" Height="221"></TextBlock>
                <TextBlock Text="element" Style="{StaticResource ColorBlack}" Height="221"></TextBlock>
            </StackPanel>
    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Monday, March 17, 2014 3:19 AM

All replies

  • Hi,

    You cannot apply this style to StackPanel. Because the TextElement.Foreground is not the Stackpanel's dependency property. You can refer to the link below:

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.style.aspx

    The link has a note like:

    In order to apply a Style, the target object must be a DependencyObject. The property that each Setter references as a Property value must be a dependency property. 

    If you want to give the TextBlock elements the color,you can define in App.XAML like below:

    <Application.Resources>
            <Style x:Key="ColorBlack" TargetType="TextBlock">
                <Setter Property="Foreground" Value="Blue"></Setter>
            </Style>
        </Application.Resources>
    <StackPanel >
                <TextBlock Text="element" Style="{StaticResource ColorBlack}" Height="221"></TextBlock>
                <TextBlock Text="element" Style="{StaticResource ColorBlack}" Height="221"></TextBlock>
            </StackPanel>
    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Monday, March 17, 2014 3:19 AM
  • Indeed, hmm i targeted the Grid to make all TextBlocks black and that works, so thank you.
    Monday, March 17, 2014 10:45 AM