Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to affect child control properties from a style

Tags:

styles

wpf

xaml

I'm creating a style that targets a button. The buttons that the style will be applied against have an image and a textblock in a stack panel inside of them. I'm looking to use a trigger to affect the properties of the child controls based upon certain condtions.

I would like to use the button style to be able to affect the stackpanels orientation as well as the image's defined width.

I've looked throught the various child control types that are available in the property intellisence of the style setter... I can see things like Grid, DockPanel and TextBlock... but the ones that I'm looking for are very noticable in their absence.

Is there a reason when I can't affect certain child control types? Is there any way to do so without rolling a custom control which explicitly exposes the child control properties that I'm looking to affect?

like image 793
Chronicide Avatar asked Apr 12 '12 16:04

Chronicide


1 Answers

You can use implicit styles:

<Style TargetType="Button" x:Key="myButtonStyle"> <!-- Has a key, will only be applied on elements that have their style set to {StaticResource myButtonStyle} -->
   <Setter Property="Background" Value="Green" />
   ...
   <Style.Resources>
      <Style TargetType="Image"> <!-- No key, so it is implicit and will apply to any child element of type Image -->
         <Setter Property="Height" Value="20" />
         ...
      </Style>
   </Style.Resources>
</Style>

Of course you can add triggers as well.

like image 73
Louis Kottmann Avatar answered Nov 03 '22 02:11

Louis Kottmann