您的位置:首页 > 其它

怎么创建silverlight custom control

2009-02-26 11:56 141 查看
这篇文章主要是介绍一下简单的Custom Control 的创建方法。

工具是vs2008

1. 首先建立一个siverlight应用程序,我这里暂时命名为CustomControl

2. 然后再建立一个Siverlight Class Library, 在这个Library工程里面建一个名字为themes的文件夹,在这个文件夹里面建立一个名字为generic.xaml的文件。

3. 修改generic.xaml文件的属性, Build Action 的属性修改成Resource, 清空Custom Tool value的值。

4. 创建一个类继承自ItemsControl,名字自己选择,我选择的是LiScrollViewer 因为我想自己弄个简单的ScrollViewer

5. 在构造函数里面制定使用样式的方式:base.DefaultStyleKey = typeof(LiScrollViewer);

6. 修改generic.xaml的内容如下

<Style x:Key="scrollviewStyle" TargetType="ScrollViewer">
<!--<Setter Property="IsEnabled" Value="true"></Setter>
<Setter Property="BorderBrush" Value="#FFA4A4A4"/>-->

</Style>
<Style TargetType="lc:LiScrollViewer">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="lc:LiScrollViewer">
<Grid x:Name="Root">
<!--<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition Width="100"></ColumnDefinition>
</Grid.ColumnDefinitions>-->
<Border>
<ScrollViewer x:Name="ScrollViewer"
Style="{StaticResource scrollviewStyle}" mce_Style="{StaticResource scrollviewStyle}"
Background="AliceBlue">
<!--<ItemsPresenter></ItemsPresenter>-->
</ScrollViewer>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>


7. 在这个CustomControl工程里面加入上个控件的引用,这样就可以在这个工程里面进行调用了。

修改工程 CustomControl里面的Page.xaml的内容如下:

<UserControl x:Class="CustomControl.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300"
xmlns:lc="clr-namespace:LiBaseControl;assembly=LiBaseControl">
<Grid x:Name="LayoutRoot" Background="White">
<lc:LiScrollViewer x:Name="ddd"></lc:LiScrollViewer>
</Grid>
</UserControl>


8. 编译这个CustomControl工程即可,然后在网页里面调用这个silverlight控件(CustomControl.xap)就可以了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: