这是我的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样式”,而不是不断重复自己?
答案 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>