WPF GroupBox Header居中

参考:https://stackoverflow.com/questions/2104013/wpf-groupbox-header-position-alignment

 

对GroupBox应用下面的Style就可以了

  <Style x:Key="GroupBoxStyle1" TargetType="{x:Type GroupBox}">
                        <Setter Property="BorderBrush" Value="#D5DFE5"/>
                        <Setter Property="BorderThickness" Value="1"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupBox}">
                                    <Grid SnapsToDevicePixels="true">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="6"/>
                                            <ColumnDefinition Width="Auto"/>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="6"/>
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="*"/>
                                            <RowDefinition Height="6"/>
                                        </Grid.RowDefinitions>
                                        <Border BorderBrush="Black" RenderTransformOrigin="0.5,0.5" BorderThickness="{TemplateBinding BorderThickness}" Grid.ColumnSpan="4" CornerRadius="4" Grid.Row="1" Grid.RowSpan="3">
                                            <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="3">
                                                <Border BorderBrush="White" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="2"/>
                                            </Border>
                                        </Border>
                                        <Border x:Name="Header" Grid.Column="2" Background="White" HorizontalAlignment="Center"  Padding="3,1,3,0" Grid.Row="0" Grid.RowSpan="2">
                                            <ContentPresenter ContentSource="Header" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                        </Border>
                                        <ContentPresenter Grid.ColumnSpan="2" Grid.Column="1" Margin="{TemplateBinding Padding}" Grid.Row="2" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                    </Grid>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>

 

效果:

 

posted @ 2020-09-19 15:03  梦醒江南·Infinite  阅读(2902)  评论(0编辑  收藏  举报