I am creating 'n' number of textblocks inside a stack panel programmatically. I need to change change the font size(both increase and decrease of font size) of 'n' textblocks. Is it possible to change font size of all child's of stack panel in single statement? If not possible how it can be solved efficiently ?
How to change the font size of all(n number of ) texblocks inside the stack panel programmatically?
Asked Answered
If you're creating the TextBlocks pragmatically, can't you set the FontSize from code when you're creating them? You might want to show some code, because it's not obvious why you'd have this problem. –
Manna
Yes, You can refer the code snippet below, where 'foobar' refers to your Stackpanel's Name.
foreach (var children in foobar.Children)
{
(children as TextBlock).FontSize = 20;
}
you're asking for nullRef exceptions with this code. –
Latinalatinate
You can apply a style in markup:
<StackPanel.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="20"/>
</Style>
</StackPanel.Resources>
Yes, You can refer the code snippet below, where 'foobar' refers to your Stackpanel's Name.
foreach (var children in foobar.Children)
{
(children as TextBlock).FontSize = 20;
}
you're asking for nullRef exceptions with this code. –
Latinalatinate
If you want all Subelements another 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.
You can use styles to apply a value to a property for all TextBlock
s inside the StackPanel
.
Sorry for the previouse wrong answer.
For some reason none of the two methods you mentioned work on Windows Phone:
TextBlock
doesn't have a SetFontSize()
method and trying to set TextBlock.FontSize
on the stack panel, the way you can do it with plain WPF, doesn't work. –
Manna @CosminPrund Are you using Silverlight in Windows Phone 7? Or something else? –
Turaco
I did File -> New -> Project -> Windows Phone -> Windows Phone App from Visual Studio 2012, whatever that defaults to. –
Manna
© 2022 - 2024 — McMap. All rights reserved.