我正在以编程方式在堆栈面板内创建'n'个文本块。我需要更改“ n”个文本块的字体大小(增加和减小字体大小)。是否可以在单个语句中更改堆栈面板的所有子面板的字体大小?如果不可能,如何有效解决?
是,您可以参考下面的代码段,[foobar]代表您Stackpanel的名称。
foreach (var children in foobar.Children)
{
(children as TextBlock).FontSize = 20;
}
您可以在标记中应用样式:
<StackPanel.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="20"/>
</Style>
</StackPanel.Resources>
您可以使用样式将TextBlock
中所有StackPanel
的值应用于属性。
对不起,先前的错误回答。
如果您希望所有子元素都具有其他样式,为什么不使用“ ContentControl”?
例如这样:
<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.