在Silverlight 2 Beta2中開發自訂控制項

來源:互聯網
上載者:User

本文主要講述如何在Silverlight2中開發一個自訂控制項,我使用環境是VS2008 Silverlight2 Beta2 。

一:建立Silverlight2 類庫項目,如下圖:

然後我們添加一個控制項類,該可以繼承自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>
效果如下圖:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.