gpt4 book ai didi

silverlight - 如何以编程方式在 Silverlight 中创建一个没有复选框(只是选中)的复选框?

转载 作者:行者123 更新时间:2023-12-04 02:34:37 25 4
gpt4 key购买 nike

作为一名 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);

感谢任何想法!

最佳答案

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

关于silverlight - 如何以编程方式在 Silverlight 中创建一个没有复选框(只是选中)的复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5962234/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com