![](/img/trans.png)
[英]Applying a style to all child elements of a certain (named) stackpanel(or control)
[英]Style all Buttons in a StackPanel the same way (UWP)
我希望StackPanel中的所有Button都具有相同的样式。
是否有类似HTML / CSS的可能性?
例如,这使HTML中的myBox类为blue的div中所有按钮的文本变为蓝色:
CSS
.myBox Button{
color: blue;
}
HTML
<div class="myBox">
<button type="button">1</button>
<button type="button">2</button>
<button type="button">3</button>
</div>
当我有了这个StackPanel时,如何将所有Button文本设为蓝色?
<StackPanel Orientation="Vertical" x:Name="ManagerMenu">
<Button x:Name="1" Content="1" />
<Button x:Name="2" Content="2" />
<Button x:Name="3" Content="3" />
</StackPanel>
您可以通过在堆栈面板中放置默认(=不带x:Key)按钮样式来做到这一点:
<StackPanel Orientation="Vertical" x:Name="ManagerMenu">
<StackPanel.Resources>
<Style TargetType="Button">
<Setter Property="Foreground" Value="Blue" />
...
</Style>
</StackPanel.Resources>
<Button x:Name="1" Content="1" />
<Button x:Name="2" Content="2" />
<Button x:Name="3" Content="3" />
</StackPanel>
但是您可能希望基于按钮的既有样式,否则它看起来会很平坦。
您可以创建一个资源标签,然后将其用于多个元素。 看看这个。 https://msdn.microsoft.com/en-us/windows/uwp/controls-and-patterns/styling-controls
如果要使用现有样式并将其应用于所有按钮:
<StackPanel>
<StackPanel.Resources>
<Style TargetType="Button" BasedOn="{StaticResource MyButtonStyle}"/>
</StackPanel.Resources>
<Button x:Name="1" Content="1" />
<Button x:Name="2" Content="2" />
<Button x:Name="3" Content="3" />
</StackPanel>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.