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

在Silverlight2 Beta2中开发自定义控件

 2008-11-07 11:47:28 来源:WEB开发网   
核心提示: <ResourceDictionaryxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx

<ResourceDictionary
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:custom="clr-namespace:CarySLCustomControlLib;assembly=CarySLCustomControlLib"
 xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows">
  <Style TargetType="custom:CarySLCustomControl">
    <Setter Property="Width" Value="200" />
    <Setter Property="Height" Value="100" />
    <Setter Property="Background" Value="Lavender" />
    <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="custom:CarySLCustomControl">
       <Grid x:Name="RootElement">
        <vsm:VisualStateManager.VisualStateGroups>
        <vsm:VisualStateGroup x:Name="CommonStates">
        <vsm:VisualStateGroup.Transitions>
        <vsm:VisualTransition To="Normal" Duration="0:0:0.2"/>
        <vsm:VisualTransition To="MouseOver" Duration="0:0:0.2"/>
        </vsm:VisualStateGroup.Transitions>
        <vsm:VisualState x:Name="Normal" />
        <vsm:VisualState x:Name="MouseOver">
        <Storyboard>
         <ColorAnimation Storyboard.TargetName="BodyElement"
             Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)"
             To="Pink" Duration="0" />
        </Storyboard>
        </vsm:VisualState>
        </vsm:VisualStateGroup>
        </vsm:VisualStateManager.VisualStateGroups>
        <Rectangle x:Name="BodyElement"
          Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"
          Fill="{TemplateBinding Background}" Stroke="Purple" RadiusX="16" RadiusY="16" />
        <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center"
          VerticalAlignment="Center" FontSize="{TemplateBinding FontSize}" />
       </Grid>
      </ControlTemplate>
    </Setter.Value>
    </Setter>
  </Style>
</ResourceDictionary>

在控件类CarySLCustomControl中添加如下代码:

public CarySLCustomControl()
{
   this.MouseEnter += new MouseEventHandler(CarySLCustomControl_MouseEnter);
   this.MouseLeave += new MouseEventHandler(CarySLCustomControl_MouseLeave);
}
void CarySLCustomControl_MouseEnter(object sender, MouseEventArgs e)
{
   VisualStateManager.GoToState(this, "MouseOver", true);
}
void CarySLCustomControl_MouseLeave(object sender, MouseEventArgs e)
{
    VisualStateManager.GoToState(this, "Normal", true);
}
这样就可以了。到此一个最简单的控件就开发好了。

上一页  1 2 3 4 5 6 

Tags:Silverlight 开发

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