用Silverlight开发GroupBox控件
最近用Silverlight 开发一个GroupBox控件,运行效果如下:
该控件主要由两部分组成代码文件GroupBox.cs和Generic.xaml,如下图所示:
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
namespace SilverlightApplication40
public class GroupBox : ContentControl
this.DefaultStyleKey = typeof(GroupBox);
public static readonly DependencyProperty TitleProperty =
DependencyProperty.Register("Title", typeof(string), typeof(GroupBox), null);
get { return (string)GetValue(TitleProperty); }
set { SetValue(TitleProperty, value); }
public static readonly DependencyProperty TitleIconProperty =
DependencyProperty.Register("TitleIcon", typeof(BitmapImage), typeof(GroupBox), null);
get { return (BitmapImage)GetValue(TitleIconProperty); }
set { SetValue(TitleIconProperty, value); }
public static readonly DependencyProperty TitleColorProperty =
DependencyProperty.Register("TitleColor", typeof(Brush), typeof(GroupBox), null);
get { return (Brush)GetValue(TitleColorProperty); }
set { SetValue(TitleColorProperty, value); }
public class ImageConverter : IValueConverter
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
Uri uri = new Uri((string)value, UriKind.RelativeOrAbsolute);
BitmapImage img = new BitmapImage(uri);
//在页面上操作的时候,将图片类型转换为数据,这里只有再TwoWay的时候才有用
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
BitmapImage img = value as BitmapImage;
return img.UriSource.AbsoluteUri;
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SilverlightApplication40"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<Style TargetType="local:GroupBox">
<Setter Property="Title" Value="标题1"></Setter>
<Setter Property="TitleColor" Value="White"></Setter>
<Setter Property="BorderBrush" Value="#61B0E9"></Setter>
<Setter Property="BorderThickness" Value="1"></Setter>
<Setter Property="Padding" Value="6,10,6,6"></Setter>
<Setter Property="FontFamily" Value="Arial,SimSun"></Setter>
<Setter Property="FontSize" Value="12"></Setter>
<Setter Property="BorderThickness" Value="1"></Setter>
<Setter Property="Foreground" Value="Black"></Setter>
<ControlTemplate TargetType="local:GroupBox">
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<Border Grid.Row="0" Background="{TemplateBinding BorderBrush}">
<StackPanel HorizontalAlignment="Left" Orientation="Horizontal">
<Image Source="{TemplateBinding TitleIcon}" Margin="3"/>
<ContentPresenter Margin="{TemplateBinding Padding}" ></ContentPresenter>
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="SilverlightApplication40.App"
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Themes/Generic.xaml"/>