Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set TargetType to this in WPF?

Tags:

wpf

xaml

I have a custom UserControl (e.g. called MyUserControl) with several child controls. I would like to create a UserControl.Resources with set of styles and dependency properties assignments for design purposes (without styles the control looks like a ball of mud). Later I will comment that styles.

The problem is, that I don't know how to set TargetType of style in order to point out the UserControl that is being developed.

Here is an example how it could look like:

<UserControl.Resources>
    <Style TargetType="this">
    </Style>
</UserControl.Resources>
like image 890
Ryszard Dżegan Avatar asked Dec 09 '25 10:12

Ryszard Dżegan


1 Answers

You can do it like this:

<UserControl x:Class="YourProjectName.UserControl1"
             xmlns:local="clr-namespace:YourProjectName"

    <UserControl.Resources>
        <Style TargetType="{x:Type local:UserControl1}">
            <Setter Property="Background" Value="Green"></Setter>
        </Style>
    </UserControl.Resources>

</UserControl>
like image 181
Nitesh Avatar answered Dec 12 '25 11:12

Nitesh



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!