属性:
外观属性
- Background:背景色
- BorderBrush:边框颜色
- BorderThickness:边框厚度
- CornerRadius:圆角半径(适用于Border控件)
内容属性
- Header:标题
- HeaderTemplate:标题模板
- HeaderStringFormat:标题字符串格式
布局属性
- Padding:内部填充
- HorizontalContentAlignment:水平内容对齐方式
- VerticalContentAlignment:垂直内容对齐方式
字体属性
- FontFamily:字体
- FontSize:字体大小
- FontStretch:字体拉伸
- FontStyle:字体样式
- FontWeight:字体粗细
其他属性
- Foreground:前景色(文本颜色)
- Opacity:不透明度
- Visibility:可见性
- IsEnabled:是否启用
完整案例:
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="CustomGroupBoxStyle" TargetType="GroupBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="GroupBox">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Border Grid.Row="1"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="5"
Padding="{TemplateBinding Padding}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Border>
<ContentPresenter Grid.Row="0"
ContentSource="Header"
RecognizesAccessKey="True"
Margin="10,0,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
ContentTemplate="{TemplateBinding HeaderTemplate}"
ContentStringFormat="{TemplateBinding HeaderStringFormat}"
Foreground="{TemplateBinding Foreground}"
FontFamily="{TemplateBinding FontFamily}"
FontSize="{TemplateBinding FontSize}"
FontStretch="{TemplateBinding FontStretch}"
FontStyle="{TemplateBinding FontStyle}"
FontWeight="{TemplateBinding FontWeight}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<GroupBox Style="{StaticResource CustomGroupBoxStyle}"
Width="300" Height="200"
Header="Custom GroupBox"
Background="LightYellow" BorderBrush="Gray"
BorderThickness="2" Padding="10">
<StackPanel>
<TextBlock Text="Content inside the GroupBox" />
<Button Content="Click Me" />
</StackPanel>
</GroupBox>
</Grid>
</Window>