【问题标题】:CheckBox Style / Format复选框样式/格式
【发布时间】: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>

【问题讨论】:

    标签: .net wpf checkbox


    【解决方案1】:
    <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>
    

    【讨论】:

    • 请回答最后一个问题。是否可以在按钮样式中包含 DataTemplate>
    • 是的,只需在 Style 之前声明 DataTemplate 并为其添加一个 setter
    猜你喜欢
    • 2011-04-18
    • 1970-01-01
    • 2019-08-01
    • 2010-12-31
    • 2019-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多