How to affect child control properties from a style

て烟熏妆下的殇ゞ 提交于 2019-12-22 03:43:52

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/10127740/how-to-affect-child-control-properties-from-a-style

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!