gpt4 book ai didi

wpf - 如何定位所有控件(WPF 样式)

转载 作者:行者123 更新时间:2023-12-03 05:11:23 27 4
gpt4 key购买 nike

我可以指定适用于所有元素的样式吗?我试过了

<Style TargetType="Control">
<Setter Property="Margin" Value="0,5" />
</Style>

但是什么也没做

最佳答案

您创建的Style仅针对Control,而不是从Control派生的元素。当您未设置 x:Key 时,它会隐式设置为 TargetType,因此在您的情况下 x:Key="{x:Type Control}"

没有任何直接的方法可以指定一个 Style 来定位从 StyleTargetType 派生的所有元素。您还有其他一些选择。

如果您有以下样式

<Style x:Key="ControlBaseStyle" TargetType="{x:Type Control}">
<Setter Property="Margin" Value="50" />
</Style>

例如,您可以定位所有按钮

<Style TargetType="{x:Type Button}" BasedOn="{StaticResource ControlBaseStyle}"/>

或直接在任何元素上使用样式,例如按钮

<Button Style="{StaticResource ControlBaseStyle}" ...>

关于wpf - 如何定位所有控件(WPF 样式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4131034/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com