WPF 基础控件之 GroupBox样式

其他基础控件

1.Window
2.Button
3.CheckBox
4.ComboBox
5.DataGrid
6.DatePicker
7.Expander

GroupBox控件修改Style需要注意使用Grid分两行进行展示第0行显示Header1行显示ContentHeader:添加Border并边框BorderThickness="1" 内部新增 ContentPresenter 然后设置ContentSource="Header"

引用Style后效果如下:

2748ea2962349bb3e9e612ae469d0658.png

Content:添加Border并边框BorderThickness="1 0 1 1" 内部新增 ContentPresenter

引用Style后效果如下:

70f2f37078da7058ed3b49e48e069e70.png

有更好的方式欢迎推荐。

01

代码如下

1)Styles.GroupBox.xaml 代码如下

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib">
    
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="../Themes/Basic/ControlBasic.xaml"/>
        <ResourceDictionary Source="../Themes/Basic/Animations.xaml"/>
    </ResourceDictionary.MergedDictionaries>

    <Style TargetType="{x:Type GroupBox}" BasedOn="{StaticResource ControlBasicStyle}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type GroupBox}">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <Border Grid.Row="0" BorderThickness="1">
                            <Border.BorderBrush>
                                <SolidColorBrush Color="{DynamicResource BaseColor}"/>
                            </Border.BorderBrush>
                            <Border.Background>
                                <SolidColorBrush Color="{DynamicResource LighterColor}"/>
                            </Border.Background>

                            <ContentPresenter Margin="10"
                                              ContentSource="Header" 
                                              RecognizesAccessKey="True" />
                        </Border>

                        <Border Grid.Row="1"
                                BorderThickness="1,0,1,1" SnapsToDevicePixels="True">
                            <Border.BorderBrush>
                                <SolidColorBrush Color="{StaticResource BaseColor}" />
                            </Border.BorderBrush>
                            <Border.Background>
                                <SolidColorBrush Color="{DynamicResource WhiteColor}"/>
                            </Border.Background>
                            <ContentPresenter Margin="4" />
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>


</ResourceDictionary>

2)使用Styles.GroupBox.xaml如下

<WrapPanel Margin="0,10">
      <GroupBox Header="GroupBox1">
                        <Rectangle Fill="{DynamicResource DangerSolidColorBrush}"
                                   Width="400" Height="300"/>
                    </GroupBox>
                    <GroupBox Header="GroupBox1" Margin="10,0" IsEnabled="False">
                        <Rectangle Fill="{DynamicResource LightSolidColorBrush}"
                                   Width="400" Height="300"/>
                    </GroupBox>
 </WrapPanel>

02


效果预览

[1][2]

参考资料

[1]

GitHub: https://github.com/WPFDevelopersOrg

[2]

Gitee: https://gitee.com/WPFDevelopersOrg

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值