wpf checkbox样式
时间: 2023-06-09 14:06:01 浏览: 172
好的,关于 WPF CheckBox 样式,你可以试试以下代码:
```xml
<Style TargetType="{x:Type CheckBox}">
<Setter Property="Foreground" Value="#FF333333"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CheckBox}">
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<Grid x:Name="gridBackground" Width="15" Height="15" Background="#FFFFFF" Margin="0,0,5,0">
<Grid.Style>
<Style TargetType="{x:Type Grid}">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Visibility" Value="Visible"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Opacity" Value="0.5"/>
</Trigger>
</Style.Triggers>
</Style>
</Grid.Style>
</Grid>
<Grid x:Name="gridBackgroundChecked" Width="15" Height="15" Margin="0,0,5,0" Visibility="Collapsed">
<Grid.Style>
<Style TargetType="{x:Type Grid}">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type CheckBox}}, Path=IsChecked}" Value="True">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Style>
<Path x:Name="pathCheckmark" Width="7" Height="5" Fill="#FF333333" Data="M1,2 L2,3 L6,0"/>
</Grid>
<ContentPresenter Margin="0"/>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="gridBackgroundChecked" Property="Visibility" Value="Visible"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="#AAAAAA"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
```
这是一个简单的 CheckBox 样式,你可以根据自己的需要进行修改。你可以将这段代码复制粘贴到你的 XAML 文件中,或者将其添加到一个 ResourceDictionary 中,便于在多个页面中进行共享。
阅读全文