用Silverlight开发GroupBox控件

最近用Silverlight 开发一个GroupBox控件,运行效果如下:

该控件主要由两部分组成代码文件GroupBox.cs和Generic.xaml,如下图所示:

以下是GroupBox.cs的内容:

using System;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Ink;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Media.Imaging;

using System.Windows.Shapes;

 

namespace SilverlightApplication40

{

 

/// <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); }

}

 

public static readonly DependencyProperty TitleIconProperty =

DependencyProperty.Register("TitleIcon", typeof(BitmapImage), typeof(GroupBox), null);

 

/// <summary>

/// 获取或设置标题图标。

/// </summary>

public BitmapImage TitleIcon

{

get { return (BitmapImage)GetValue(TitleIconProperty); }

set { SetValue(TitleIconProperty, value); }

}

 

public static readonly DependencyProperty TitleColorProperty =

DependencyProperty.Register("TitleColor", typeof(Brush), typeof(GroupBox), null);

 

/// <summary>

/// 获取或设置标题颜色。

/// </summary>

public Brush TitleColor

{

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)

{

try

{

Uri uri = new Uri((string)value, UriKind.RelativeOrAbsolute);

BitmapImage img = new BitmapImage(uri);

return img;

}

catch

{

return new BitmapImage();

}

}

 

//在页面上操作的时候,将图片类型转换为数据,这里只有再TwoWay的时候才有用

public object ConvertBack(object value, Type targetType, object parameter,

System.Globalization.CultureInfo culture)

{

BitmapImage img = value as BitmapImage;

return img.UriSource.AbsoluteUri;

}

}

}

以下是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: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>

 

<Setter Property="Template">

<Setter.Value>

<ControlTemplate TargetType="local:GroupBox">

 

<Grid>

<Grid.RowDefinitions>

<RowDefinition Height="Auto"/>

<RowDefinition Height="Auto"/>

</Grid.RowDefinitions>

<Border Grid.Row="0" Background="{TemplateBinding BorderBrush}">

<StackPanel HorizontalAlignment="Left" Orientation="Horizontal">

<Image Source="{TemplateBinding TitleIcon}" Margin="3"/>

<TextBlock Margin="3" Text="{TemplateBinding Title}" Foreground="{TemplateBinding TitleColor}" FontFamily="{TemplateBinding FontFamily}" FontSize="{TemplateBinding FontSize}" FontWeight="Bold" VerticalAlignment="Center" />

</StackPanel>

</Border>

<Border Grid.Row="1" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">

<ContentPresenter Margin="{TemplateBinding Padding}" ></ContentPresenter>

</Border>

</Grid>

 

 

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>

 

</ResourceDictionary>

最后别忘记在App.xaml中加入以下内容

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

x:Class="SilverlightApplication40.App"

>

<Application.Resources>

<ResourceDictionary>

<ResourceDictionary.MergedDictionaries>

<ResourceDictionary Source="Themes/Generic.xaml"/>

</ResourceDictionary.MergedDictionaries>

</ResourceDictionary>

</Application.Resources>

</Application>

posted @ 2014-03-31 16:16  liuyunfeng  阅读(361)  评论(0编辑  收藏  举报