Silverlight没有提供GroupBox控件,自己动手写了一个。
Generic.xaml文件:
- <ResourceDictionary
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="clr-namespace:Sample"
- xmlns:sys="clr-namespace:System;assembly=mscorlib">
- <Style TargetType="local:GroupBox">
- <Setter Property="Background" Value="White"></Setter>
- <Setter Property="BorderBrush" Value="#687B8B"></Setter>
- <Setter Property="BorderThickness" Value="1"></Setter>
- <Setter Property="Padding" Value="6,10,6,6"></Setter>
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="local:GroupBox">
- <Grid>
- <Border Margin="0,8,0,0" CornerRadius="5"
- Background="{TemplateBinding Background}"
- BorderBrush="{TemplateBinding BorderBrush}"
- BorderThickness="{TemplateBinding BorderThickness}">
- <ContentPresenter Margin="{TemplateBinding Padding}" ></ContentPresenter>
- </Border>
- <Border Margin="10,0,10,0" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Top"
- Background="{TemplateBinding Background}" >
- <TextBlock Margin="5,0" Text="{TemplateBinding Title}" ></TextBlock>
- </Border>
- </Grid>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
- </ResourceDictionary>
GroupBox.cs文件:
- using System.Windows;
- using System.Windows.Controls;
- namespace Sample
- {
- /// <summary>
- /// 分组框。
- /// </summary>
- public class GroupBox : ContentControl
- {
- public GroupBox()
- {
- this.DefaultStyleKey = typeof(GroupBox);
- }
- public static readonly DependencyProperty TitleProperty =
- DependencyProperty.Register("Title", typeof (string), typeof (GroupBox), null);
- /// <summary>
- /// 获取或设置标题。
- /// </summary>
- public string Title
- {
- get { return (string) GetValue(TitleProperty); }
- set { SetValue(TitleProperty, value); }
- }
- }
- }
使用示例代码:
- <UserControl x:Class="AutoCompleteBoxSample.GroupBoxSample"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="clr-namespace:Sample" Width="400" Height="300">
- <Grid x:Name="LayoutRoot" Margin="30" Background="White">
- <local:GroupBox Title="GroupBox的标题" >
- <Button Content="GroupBox的内容"></Button>
- </local:GroupBox>
- </Grid>
- </UserControl>
示例效果图:
