WPF CheckBox可以使用样式来美化其外观。以下是一个简单的WPF CheckBox样式:

<Window.Resources>
    <Style x:Key='CheckBoxStyle1' TargetType='{x:Type CheckBox}'>
        <Setter Property='Foreground' Value='#333' />
        <Setter Property='FontSize' Value='14' />
        <Setter Property='Template'>
            <Setter.Value>
                <ControlTemplate TargetType='{x:Type CheckBox}'>
                    <StackPanel Orientation='Horizontal'>
                        <Border BorderBrush='#333' BorderThickness='1' CornerRadius='3' Width='25' Height='25' Margin='0,0,10,0'>
                            <Grid>
                                <Ellipse x:Name='CheckBackground' Fill='White' StrokeThickness='2' Stroke='#333' />
                                <Ellipse x:Name='CheckMark' Fill='#333' Opacity='0' />
                            </Grid>
                        </Border>
                        <ContentPresenter />
                    </StackPanel>
                    <ControlTemplate.Triggers>
                        <Trigger Property='IsChecked' Value='True'>
                            <Setter TargetName='CheckMark' Property='Opacity' Value='1' />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

<Grid>
    <CheckBox Content='My CheckBox' Style='{StaticResource CheckBoxStyle1}' />
</Grid>

此样式使用了一个横向堆叠的StackPanel,其中包含一个边框和一个ContentPresenter。边框包含一个Grid,其中包含两个椭圆形,用于表示选中状态和未选中状态。使用Opacity属性来控制选中状态的显示和隐藏。ControlTemplate.Triggers用于在IsChecked属性为True时触发选中状态的显示。

WPF CheckBox 样式美化:自定义外观指南

原文地址: https://www.cveoy.top/t/topic/noeG 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录