Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF Beginner Style
I would like to do something like this:
 
<Style TargetType="Control">
  <Setter Property="VerticalAlignment" Value="Center"></Setter>
</Style>
 
Is this possible? It doesn't work this way...
I know I could write for every control something like the following:
 
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource M<ControlBaseStyle}"/>
 
But I have 10 different controls and I don't really want to do this.
 
Any ideas?
 
Thank you!
Markus
Posted 22-Aug-12 3:18am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can't do this, but you can create one style and then use it for other controls.
 
If you have the following Style
<Style x:Key="ControlBaseStyle" TargetType="{x:Type Control}">
    <Setter Property="Margin" Value="50" />
</Style>
 
You can target all Buttons for instance
 
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource ControlBaseStyle}"/>
 
You still need to add the style for each control type, but you can make it so the actual style is only there once.
  Permalink  
Comments
NeonMika at 22-Aug-12 9:26am
   
Okay, so I have to write down the "tree". Thanks for your answer, 5*.
Christian Graus at 22-Aug-12 9:27am
   
glad to help :-)

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

  Print Answers RSS
0 CPallini 355
1 BillWoodruff 334
2 George Jonsson 279
3 Sergey Alexandrovich Kryukov 243
4 Gihan Liyanage 225
0 OriginalGriff 5,095
1 CPallini 4,285
2 Sergey Alexandrovich Kryukov 3,767
3 George Jonsson 2,941
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 22 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100