King Chan King Chan - 8 months ago 72
C# Question

Can I have one Style with multiple TargetType in WPF?

As titled, and I mean something like below:

<Style TargetType="{x:Type TextBlock}"
TargetType="{x:Type Label}"
TargetType="{x:Type Button}" >

This is actually for the sake of using a 3rd party control, I have inherited their class. But the template doesn't apply to the SubClass because the
is on the base class. So I would like to set multiple
s to make it able to apply for both.


No you cannot, however I often create a style for a shared base class such as FrameworkElement, and then create my individual control styles that are BasedOn the base style

<Style TargetType="{x:Type FrameworkElement}">
    <!-- Shared Setters -->

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