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
774 views
in Technique[技术] by (71.8m points)

wpf - Setting a style's TargetType property to a base class

I was just poking around a bit in WPF and wanted all elements on my Window to share the same margin. I found that all Controls that are capable of having a margin derive from FrameworkElement so I tried the following:

<Window.Resources>
<Style TargetType="{x:Type FrameworkElement}">
    <Setter Property="Margin" Value="10" />
</Style>
</Window.Resources>

And, this doesn't work. I can apply this to all Buttons, but not to all Elements that derive from Button. Am I missing something or is this simply not possible?

Am I the only one feeling like using CSS for WPF would have been a good idea?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

Unfortunately, you cannot apply styles to the base FrameworkElement type; while WPF will let you write the style, it will not apply it to the controls that derive from it. It appears that this also applies to subtypes of FrameworkElement, e.g. ButtonBase, the supertype of Button/ToggleButton/RepeatButton.

You can still use inheritance, but you will have to use the explicit BasedOn syntax to apply it to the control types you want it to apply to.

<Window.Resources>
    <Style TargetType="{x:Type FrameworkElement}">
        <Setter Property="Margin" Value="10" />
    </Style>

    <Style TargetType="{x:Type Label}" BasedOn="{StaticResource {x:Type FrameworkElement}}" />
    <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type FrameworkElement}}" />
    <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type FrameworkElement}}" />

</Window.Resources>


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

...