热门:网页模板.net视频教程JQueryMVCjsonExtJs源码示例三级联动JQuery菜单
您现在的位置:.Net中文社区>> Silverlight>>正文内容

[Silverlight]一个简单的GroupBox控件

发布时间:2010年04月27日点击数: 佚名

Silverlight没有提供GroupBox控件,自己动手写了一个。
Generic.xaml文件:

  1. <ResourceDictionary 
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  4.     xmlns:local="clr-namespace:Sample"  
  5.     xmlns:sys="clr-namespace:System;assembly=mscorlib"> 
  6.  
  7.     <Style TargetType="local:GroupBox"> 
  8.         <Setter Property="Background" Value="White"></Setter> 
  9.         <Setter Property="BorderBrush" Value="#687B8B"></Setter> 
  10.         <Setter Property="BorderThickness" Value="1"></Setter> 
  11.         <Setter Property="Padding" Value="6,10,6,6"></Setter> 
  12.         <Setter Property="Template"> 
  13.             <Setter.Value> 
  14.                 <ControlTemplate TargetType="local:GroupBox"> 
  15.                     <Grid> 
  16.                         <Border Margin="0,8,0,0" CornerRadius="5" 
  17.                                 Background="{TemplateBinding Background}" 
  18.                                 BorderBrush="{TemplateBinding BorderBrush}" 
  19.                                 BorderThickness="{TemplateBinding BorderThickness}"> 
  20.                             <ContentPresenter Margin="{TemplateBinding Padding}" ></ContentPresenter> 
  21.                         </Border> 
  22.                         <Border Margin="10,0,10,0" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Top" 
  23.                                 Background="{TemplateBinding Background}" > 
  24.                             <TextBlock Margin="5,0" Text="{TemplateBinding Title}" ></TextBlock> 
  25.                         </Border> 
  26.                     </Grid> 
  27.                 </ControlTemplate> 
  28.             </Setter.Value> 
  29.         </Setter> 
  30.     </Style> 
  31. </ResourceDictionary> 

GroupBox.cs文件:

  1. using System.Windows; 
  2. using System.Windows.Controls; 
  3.  
  4. namespace Sample 
  5.     /// <summary> 
  6.     /// 分组框。 
  7.     /// </summary> 
  8.     public class GroupBox : ContentControl 
  9.     { 
  10.         public GroupBox() 
  11.         { 
  12.             this.DefaultStyleKey = typeof(GroupBox); 
  13.         } 
  14.  
  15.         public static readonly DependencyProperty TitleProperty = 
  16.             DependencyProperty.Register("Title"typeof (string), typeof (GroupBox), null); 
  17.  
  18.         /// <summary> 
  19.         /// 获取或设置标题。 
  20.         /// </summary> 
  21.         public string Title 
  22.         { 
  23.             get { return (string) GetValue(TitleProperty); } 
  24.             set { SetValue(TitleProperty, value); } 
  25.         } 
  26.     } 

使用示例代码:

  1. <UserControl x:Class="AutoCompleteBoxSample.GroupBoxSample" 
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.     xmlns:local="clr-namespace:Sample" Width="400" Height="300"> 
  5.     <Grid x:Name="LayoutRoot" Margin="30" Background="White"> 
  6.         <local:GroupBox Title="GroupBox的标题" > 
  7.             <Button Content="GroupBox的内容"></Button>                 
  8.         </local:GroupBox> 
  9.     </Grid> 
  10. </UserControl> 

示例效果图:

image

本站热点业务

更多模板/案例展示

关于我们 | 联系我们 | 团队日志 | 网站地图 | 网站合作