如何定位所有控件(WPF 样式) [英] How to target all controls (WPF Styles)

查看:26
本文介绍了如何定位所有控件(WPF 样式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can I specify a style that applies to all elements? I tried

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

但它什么也没做

推荐答案

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

The Style you created is only targeting Control and not elements that derive from Control. When you don't set the x:Key it's implicitly set to the TargetType, so in your case x:Key="{x:Type Control}".

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

There isn't any direct way to specify a Style that targets all elements that derive from the TargetType of the Style. You have some other options.

如果你有以下Style

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

例如,您可以定位所有Buttons

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

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

or use the style directly on any element, e.g. Button

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

这篇关于如何定位所有控件(WPF 样式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆