How to apply style for all control in panel [duplicate]
Possible Duplicate:
How to target all controls (WPF Styles)
My panel contains many kind of control such as Button, TextBox
,.. and all of them derived from Control
. I want to set margin for all of them, I hoped that d开发者_如何学Goefining style for Control
would automatically set property to all controls, but it does not work.
<WrapPanel>
<WrapPanel.Resources>
<Style TargetType="Control">
<Setter Property="Margin" Value="5,5,0,0" />
</Style>
</WrapPanel.Resources>
<Button Content="Button 1" />
<Button Content="Button 2" />
<TextBox Width="100" />
</WrapPanel>
unfortunately, I do not think that is possible :( Also see: How to target all controls (WPF Styles)
There is one small error in your xaml code above, it should say TargetType="{x:Type Control}" for the xaml parser to set a Type object (and not a string).
So you either have to create a style for every control type. One more option could be to use style inheritance (based on another style), but I think the number of code lines will be the same.
Hope that helped a little.
精彩评论