0

我有以下风格

<Style x:Key="LockBox" TargetType="CheckBox">
    <Style.Triggers>
        <Trigger Property="IsChecked" Value="True">
            <!-- Locked -->
            <Setter Property="Content" Value="&#x1f512;" />
        </Trigger>
        <Trigger Property="IsChecked" Value="False">
            <!-- Unlocked -->
            <Setter Property="Content" Value="&#x1f513;" />
        </Trigger>
    </Style.Triggers>
</Style>

这会将 unicode 锁定/解锁符号设置为复选框内容。但是,我想将锁定/解锁符号放在图像所在的位置。这该怎么做?

4

1 回答 1

3

这是可用于实现目标的 ControlTemplate 的完整示例。

<ControlTemplate x:Key="CheckBoxTemplate" TargetType="{x:Type CheckBox}">
    <Grid Background="{TemplateBinding Background}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="24" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid
            Grid.Column="0" Grid.Row="0"
            Width="24" Height="24"
            VerticalAlignment="Center">
            <TextBlock Text="&#x1f512;" Name="LockedIcon" Visibility="Hidden" />
            <TextBlock Text="&#x1f513;" Name="UnlockedIcon" Visibility="Hidden"/>
            </Grid>
        <Border 
            x:Name="WhiteSpaceBorder"
            Grid.Column="1" Grid.Row="0" 
            Width="8" 
            Visibility="Visible"/>
        <ContentPresenter 
            Grid.Column="2" Grid.Row="0"
            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
            Margin="{TemplateBinding Padding}"
            RecognizesAccessKey="True" 
            TextBlock.Foreground="{TemplateBinding Foreground}"/>
    </Grid>
    <ControlTemplate.Triggers>
        <Trigger Property="IsChecked" Value="True">
            <!-- Locked -->
            <Setter Property="Visibility" TargetName="LockedIcon" Value="Visible" />
        </Trigger>
        <Trigger Property="IsChecked" Value="False">
            <!-- Unlocked -->
            <Setter Property="Visibility" TargetName="UnlockedIcon" Value="Visible" />
        </Trigger>
        <Trigger Property="Content" Value="{x:Null}">
            <Setter Property="Visibility" TargetName="WhiteSpaceBorder" Value="Collapsed"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>
于 2013-05-27T11:23:25.967 回答