Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 复选框样式/格式_.net_Wpf_Checkbox - Fatal编程技术网

.net 复选框样式/格式

.net 复选框样式/格式,.net,wpf,checkbox,.net,Wpf,Checkbox,有没有办法通过复选框上的样式(而不是文本块)来实现这一点 e、 g 基于Pasha的回答 填充会移动文本 这里的问题是样式应用除填充之外的所有属性 如下所示:CB1和CB2没有相同的填充 ContentTemplate确实有效 是否可以在按钮样式中包含ContentTemplate <Window x:Class="CheckBoxStyle.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xam

有没有办法通过复选框上的样式(而不是文本块)来实现这一点


e、 g


基于Pasha的回答
填充会移动文本
这里的问题是样式应用除填充之外的所有属性
如下所示:CB1和CB2没有相同的填充
ContentTemplate确实有效
是否可以在按钮样式中包含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>



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



请回答最后一个问题。是否可以在按钮样式中包含DataTemplate>是,只需在样式之前声明DataTemplate并为其添加一个setter即可
<CheckBox Content="All" Style="{StaticResource CloseText}" />
<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>
<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>