CheckBox样式/格式

时间:2014-02-13 15:34:01

标签: .net wpf checkbox

有没有办法通过CheckBox(不是TextBlock)

上的样式来实现这一点
<CheckBox>
  <TextBlock Text="All" Margin="-1,1,0,0" />
</CheckBox>

e.g。

<CheckBox Content="All" Style="{StaticResource CloseText}" />

根据Pasha的答案 填充确实会移动文本
这里的问题是Style应用所有属性但填充
见下文: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>

1 个答案:

答案 0 :(得分:2)

<Window.Resources>
    <DataTemplate x:Key="CloseText">
        <TextBlock Text="{Binding}" Margin="-1,1,0,0" />
    </DataTemplate>
</Window.Resources>

...

<CheckBox Content="All" ContentTemplate="{StaticResource CloseText}"/>

作为复选框样式的一部分(内联或资源引用):

    <DataTemplate x:Key="CloseText">
        <TextBlock Text="{Binding}" Margin="-3,1,0,0" />
    </DataTemplate>
    <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" />
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <TextBlock Text="{Binding}" Margin="-3,1,0,0" />
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </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" />
        <Setter Property="ContentTemplate" Value="{StaticResource CloseText}" />
    </Style>