WEB开发网
开发学院图形图像Flash 在Silverlight2 Beta2中开发自定义控件 阅读

在Silverlight2 Beta2中开发自定义控件

 2008-11-07 11:47:28 来源:WEB开发网   
核心提示:一:创建Silverlight2 类库项目,如下图: 然后我们添加一个控件类,在Silverlight2 Beta2中开发自定义控件,该可以继承自Control类,也可以继承自其他类比如ContentControl,ItemControl,我们继承自ContentControl

一:创建Silverlight2 类库项目,如下图:

在Silverlight2 Beta2中开发自定义控件

然后我们添加一个控件类,该可以继承自Control类,也可以继承自其他类比如ContentControl,ItemControl。我们继承自ContentControl,代码如下:

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace CarySLCustomControlLib
{
  public class CarySLCustomControl : ContentControl
  {}
}

其实现在已经做好了一个最简单的自定义控件,我们给给他一个控件模板就可以了。在Page.xaml的Grid中添加如下代码:

<custom:CarySLCustomControl>
  <custom:CarySLCustomControl.Template>
   <ControlTemplate>
    <Grid x:Name="RootElement">
      <Rectangle x:Name="BodyElement" Width="200" Height="100"
                 Fill="Lavender" Stroke="Purple" RadiusX="16" RadiusY="16" />
      <TextBlock Text="Cary Click" HorizontalAlignment="Center"VerticalAlignment="Center" />
    </Grid>
   </ControlTemplate>
  </custom:CarySLCustomControl.Template>
</custom:CarySLCustomControl>
效果如下图:

1 2 3 4 5 6  下一页

Tags:Silverlight 开发

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接