WPF:我可以同时将相同的样式应用于多个项目吗?

时间:2010-07-18 12:37:26

标签: wpf styles

这是我的XAML片段:

<StackPanel Orientation="Horizontal" >
    <CheckBox Content="Sunday" IsChecked="{Binding Sunday}"  Style="{StaticResource ResourceKey=GridChecks}"/>
    <CheckBox Content="Monday" IsChecked="{Binding Monday}"  Style="{StaticResource ResourceKey=GridChecks}"/>
    <CheckBox Content="Tuesday" IsChecked="{Binding Tuesday}"  Style="{StaticResource ResourceKey=GridChecks}"/>
    <CheckBox Content="Wednesday" IsChecked="{Binding Wednesday}"  Style="{StaticResource ResourceKey=GridChecks}"/>
    <CheckBox Content="Thursday" IsChecked="{Binding Thursday}"  Style="{StaticResource ResourceKey=GridChecks}"/>
    <CheckBox Content="Friday" IsChecked="{Binding Friday}"  Style="{StaticResource ResourceKey=GridChecks}"/>
    <CheckBox Content="Saturday" IsChecked="{Binding Saturday}"  Style="{StaticResource ResourceKey=GridChecks}"/>
</StackPanel>

有没有办法说“堆栈面板下的所有复选框都能获得GridChecks样式”,而不是不断重复自己?

1 个答案:

答案 0 :(得分:4)

<StackPanel>
   <StackPanel.Resources>
      <Style TargetType={x:Type CheckBox}>
      <!--define your checkbox style here-->
      </Style>
   </StackPanel.Resources>

   <!--these checkboxes will have defined style described in StackPanel.Resources-->
   <CheckBox Content="First"/>
   <CheckBox Content="Second"/>
   <CheckBox Content="Third"/>
</StackPanel>