如何以编程方式在Silverlight中创建没有框(只是检查)的复选框?

时间:2011-05-11 09:44:12

标签: silverlight controls checkbox

作为一名银光新手,我想在silverlight4中创建一个无盒装的只读复选框,以显示绿色复选标记。我无法让盒子看不见/透明或使勾选标记为绿色,它会保持灰色。

我尝试了什么:

        cbstatus = new CheckBox();
        cbstatus.IsEnabled = false; // read only
        cbstatus.Visibility = System.Windows.Visibility.Visible;
        cbstatus.Background =  new SolidColorBrush(Colors.Transparent);
        cbstatus.BorderBrush = new SolidColorBrush(Colors.Transparent);
        cbstatus.Foreground = new SolidColorBrush(Colors.Green);

感谢您的任何想法!

1 个答案:

答案 0 :(得分:2)

您需要覆盖默认模板。将以下样式添加到App.xaml资源中作为起点: -

<Style x:Key="BorderlessReadOnlyCheckBox" TargetType="CheckBox">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="Foreground" Value="#FF000000"/>
    <Setter Property="HorizontalContentAlignment" Value="Left"/>
    <Setter Property="VerticalContentAlignment" Value="Top"/>
    <Setter Property="Padding" Value="4,1,0,0"/>
    <Setter Property="IsEnabled" Value="False" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="CheckBox">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="16"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CheckStates">
                            <VisualState x:Name="Checked">
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetName="CheckIcon" Storyboard.TargetProperty="(UIElement.Opacity)" Duration="0" To="1"/>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Unchecked"/>
                            <VisualState x:Name="Indeterminate"/>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Grid HorizontalAlignment="Left" VerticalAlignment="Top" Width="16" Height="16">
                        <Path x:Name="CheckIcon" Margin="1,1,0,1.5" Fill="Green" Stretch="Fill" Opacity="0" Width="10.5" Height="10" Data="M102.03442,598.79645 L105.22962,597.78918 L106.78825,600.42358 C106.78825,600.42358 108.51028,595.74304 110.21724,593.60419 C112.00967,591.35822 114.89314,591.42316 114.89314,591.42316 C114.89314,591.42316 112.67844,593.42645 111.93174,594.44464 C110.7449,596.06293 107.15683,604.13837 107.15683,604.13837 z" FlowDirection="LeftToRight"/>
                    </Grid>
                    <ContentPresenter
                          Grid.Column="1"
                          x:Name="contentPresenter"
                          Content="{TemplateBinding Content}"
                          ContentTemplate="{TemplateBinding ContentTemplate}"
                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                          Margin="{TemplateBinding Padding}"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

现在您设置复选框的样式: -

cbstatus = new CheckBox();
cbstatus.Style = (Style)Application.Current.Resources["BorderlessReadOnlyCheckBox"];