Click here to Skip to main content
15,888,003 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Its possible ?

I don't understand why this code works fine:

<Style x:Key="MyStyle" TargetType="{x:Type Canvas}">
<Setter Property="Width" Value="125" />
<Setter Property="Height" Value="55" />
<Setter Property="Background" Value="White" />
</Style>

But this one its wrong:

<Style x:Key="MyStyle">
<Style.Resources>
<Style TargetType="Canvas">
<Setter Property="Width" Value="125" />
<Setter Property="Height" Value="55" />
<Setter Property="Background" Value="White" />
</Style>
</Style.Resources>
</Style>

What I have tried:

The case is I want to inject several types in this last MyStyle, such as textboxes, ellipses, etc..

I explained myself well?
Posted
Updated 15-Jan-18 20:39pm
Comments
[no name] 12-Jan-18 8:38am    
No please explain properly.

1 solution

If you want the same style to work with multiple types, you actually need to create a style that targets a base type of those controls. You don't have to constrain yourself to the most derived type. For instance, you could define a style for all Panel types (Canvas, etc), like this:
C#
<Style x:Key="PanelStyle" TargetType="{x:Type Panel}">
  <Setter Property="Width" Value="125" />
  <Setter Property="Height" Value="55" />
</Style>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900