Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
999 views
in Technique[技术] by (71.8m points)

user interface - How to target all controls (WPF Styles)

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

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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}".

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.

If you have the following Style

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

You can target all Buttons for example

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

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...