How to programmatically change the font size for all (n number) of texblocks inside the stack panel?

I create an "n" number of text blocks inside the stack pane programmatically. I need to change the font size (both increase and decrease the font size) of the text blocks "n". Is it possible to change the font size of the entire stack child panel in one expression? If this is not possible, then how can this be effectively solved?

+6
source share
4 answers

Yes, you can link to the code snippet below where "foobar" refers to your Stackpanel name.

        foreach (var children in foobar.Children)
        {
            (children as TextBlock).FontSize = 20;
        }
+4
source

You can apply the style in the markup:

<StackPanel.Resources>
<Style TargetType="TextBlock">
  <Setter Property="FontSize" Value="20"/>
</Style>
</StackPanel.Resources>
+10
source

TextBlock StackPanel.

.

+1

If you want all Subelements to use a different style, why not use ContentControl?

For example, like this:

    <GroupBox Header="Some Header" FontSize="18" FontWeight="Bold">
        <ContentControl FontSize="14" FontWeight="Normal">
        ....
        </ContentControl
    <GroupBox>

All elements inside the ContentControl Block will be st with normal weight and a size of 14.
0
source

All Articles