【问题标题】:How do I create a checkbox without the box (just the check) in Silverlight programmatically?如何以编程方式在 Silverlight 中创建一个没有框(只是复选)的复选框?
【发布时间】:2011-08-23 03:56:41
【问题描述】:

作为 silverlight 新手,我想在 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);

感谢您的任何想法!

【问题讨论】:

    标签: silverlight controls checkbox


    【解决方案1】:

    您需要覆盖默认模板。将以下样式添加到您的 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"];
    

    【讨论】:

    • 我的项目中没有App.xml,而是一个Styles.xml(由VS模板创建),所以我在那里添加了样式。但是还有一个问题,我的Application对象不包含Resources集合,所以无法访问样式
    • @elsni:您是通过在新项目对话框中选择新的“Silverlight 应用程序”来启动项目的,对吧?该模板包含一个初始 App.xaml 文件。
    • 抱歉,我忽略了这一点。它肯定包含一个 App.xml,它合并了 Styles.xml。但是没有 Application.Resources :-/
    猜你喜欢
    • 1970-01-01
    • 2010-11-27
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2011-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多