如何定位所有控件(WPF 样式)
我可以指定适用于所有元素的样式吗?我尝试过
<Style TargetType="Control">
<Setter Property="Margin" Value="0,5" />
</Style>
但什么也没做
Can I specify a style that applies to all elements? I tried
<Style TargetType="Control">
<Setter Property="Margin" Value="0,5" />
</Style>
But it did nothing
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您创建的
Style
仅针对Control
,而不是从Control
派生的元素。当您未设置x:Key
时,它会隐式设置为TargetType
,因此在您的情况下x:Key="{x:Type Control}"
。没有任何直接的方法可以指定一个
Style
来定位从Style
的TargetType
派生的所有元素。您还有其他一些选择。例如,如果您有以下
Style
您可以定位所有
Button
或直接在任何元素上使用该样式,例如
Button
The
Style
you created is only targetingControl
and not elements that derive fromControl
. When you don't set thex:Key
it's implicitly set to theTargetType
, so in your casex:Key="{x:Type Control}"
.There isn't any direct way to specify a
Style
that targets all elements that derive from theTargetType
of theStyle
. You have some other options.If you have the following
Style
You can target all
Buttons
for exampleor use the style directly on any element, e.g.
Button
正如 Fredrik Hedblad 回答的那样,您可以影响从控制继承的所有元素。
但是,例如,您不能将样式应用于具有相同样式的文本块和按钮。
这样做:
As Fredrik Hedblad answered you can effect all elements that inherited from control.
But you can't apply style for textblock and button with the same style for example.
to do that: