WPF:可以将Style应用于StackPanel中的所有项目,无论其类型如何

时间:2013-06-01 12:24:27

标签: wpf styles wpf-controls

如果StackPanel包含多种类型的控件(例如ComboBox,CheckBox,Grid),则在应用边距或填充样式时,XAML中是否可以表达“您包含的所有项目”或“每个项目”的概念你包含“并将样式应用于StackPanel本身,或者每个控件必须由其类型明确引用,如下所示?有没有办法说TargetType = ANY?

<StackPanel>
   <StackPanel.Resources>
     <Style TargetType="{x:Type foo:ComboBox}">
         <Setter Property="Margin" Value="10,10,10,10"/>
     </Style>
     <Style TargetType="{x:Type foo:CheckBox}">
         <Setter Property="Margin" Value="10,10,10,10"/>
     </Style>
      <Style TargetType="{x:Type foo:GridView}">
         <Setter Property="Margin" Value="10,10,10,10"/>
     </Style>
</StackPanel.Resources>

1 个答案:

答案 0 :(得分:0)

<StackPanel>
    <StackPanel.Resources>
        <Style TargetType="{x:Type Control}">
            <Style.Setters>
                <Setter Property="Margin" Value="5"/>
            </Style.Setters>
        </Style>
    </StackPanel.Resources>
    <ComboBox />
    <Label />
    <ListView />