【发布时间】:2014-02-14 00:39:53
【问题描述】:
有没有办法通过 CheckBox(不是 TextBlock)上的样式来实现这一点
<CheckBox>
<TextBlock Text="All" Margin="-1,1,0,0" />
</CheckBox>
例如
<CheckBox Content="All" Style="{StaticResource CloseText}" />
根据帕夏的回答
填充确实移动了文本
这里的问题是样式应用了所有属性,但填充
见下文:CB1 和 CB2 没有相同的填充
ContentTemplate 确实有效
是否可以在 Button 样式中包含 ContentTemplate?
<Window x:Class="CheckBoxStyle.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="CheckBox">
<Setter Property="FontFamily" Value="Segoe UI" />
<Setter Property="FontStyle" Value="Italic" />
<Setter Property="Margin" Value="50,2,0,0" />
<Setter Property="Padding" Value="-1,0,0,0" />
</Style>
<Style TargetType="CheckBox" x:Key="CheckBox01">
<Setter Property="FontFamily" Value="Courier" />
<Setter Property="FontStyle" Value="Oblique" />
<Setter Property="Margin" Value="40,2,0,0" />
<Setter Property="Padding" Value="10,0,0,0" />
</Style>
<DataTemplate x:Key="CloseText">
<TextBlock Text="{Binding}" Margin="-3,1,0,0" />
</DataTemplate>
</Window.Resources>
<Grid>
<StackPanel Orientation="Vertical">
<CheckBox Content="CB1" />
<CheckBox Content="CB2" Padding="-1,0,0,0" />
<CheckBox Content="CB3" Style="{StaticResource CheckBox01}" />
<CheckBox Content="CB4" Style="{StaticResource CheckBox01}" ContentTemplate="{StaticResource CloseText}"/>
</StackPanel>
</Grid>
</Window>
【问题讨论】: