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后效果如下:

WPF 基础控件之 GroupBox样式

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

引用Style后效果如下:

WPF 基础控件之 GroupBox样式

有更好的方式欢迎推荐。

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


原文始发于微信公众号(WPF开发者):WPF 基础控件之 GroupBox样式

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/55221.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!