如何从样式影响子控件属性 [英] How to affect child control properties from a style
问题描述
我正在创建一个以按钮为目标的样式.将应用该样式的按钮在其内部的堆栈面板中有一个图像和一个文本块.我希望使用触发器来根据某些条件影响子控件的属性.
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.
我已经查看了样式设置器的属性智能中可用的各种子控件类型......我可以看到诸如 Grid、DockPanel 和 TextBlock 之类的东西......但我正在寻找的是在他们缺席的情况下非常引人注目.
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?
推荐答案
您可以使用隐式样式:
<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.
这篇关于如何从样式影响子控件属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!