Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Styling all elements using UIElement

Here's something I want to do:

<Application.Resources>
    <Style TargetType="{x:Type UIElement}">
        <Setter Property="Opacity"
                Value=".1" />
    </Style>
</Application.Resources>

So that I can style any type (not just some final concrete UI type). I am not looking for best practices, its more of a question to ponder.

I noticed that WPF does not style any super class specified in TargetType (UIElement, FrameworkElement, etc etc). It styles only if the TargetType equates to the concrete UI class (Button, Rectangle).

like image 545
Payal D'Cruz Avatar asked Apr 28 '26 13:04

Payal D'Cruz


1 Answers

if you just want to define a base Style, you can however use BasedOn property.

<Style TargetType="FrameworkElement" x:Key="ElementBase">
    <Setter Property="Height" Value="24"/>
</Style>

<Style TargetType="TextBlock" BasedOn="{StaticResource ElementBase}">
</Style>

It is a little bit more work, but maybe it helps.

like image 101
exception Avatar answered May 01 '26 04:05

exception



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!