快捷搜索:

Silverlight中的使用装饰器模式的变换操作

在模式设计中,应用装饰器模式来给现有的工具作为附加的功能添加到原工具中,亦即在不改动源工具的环境下给源工具添加一些特定的行径,达到我们富厚源工具功能的目的,幸好WPF的开创人给我们设计了响应的类型,我们可以直接应用诸如Adorner,AdornerLayer,AdornerDecorator等类型很轻易地达到这个目的。很遗憾的是到今朝为止,笔者还没有找到在微软的.net专家们为Silverlight 开拓了类似的类库。于是我在借鉴了国外的一些开拓专家的履历,设计了一个类库来达到这个目的,下面将这个例子分享大年夜家。

自定义的RotateAndResizingLib.dll类库的目的是为了实现两个操作,正如名字所说的亦即扭转和缩放工具操作。

因为我们的目的是要对可视的元素实现这样的操作,我们构建一个Adorner的用户控件,这个一个承袭自UserControl的一个类型:我们的设法主见是在源工具上加入一个两个不合外形的Thumb工具来实现扭转和缩放操作。这两个Thumb分手添加到源工具的左上角和右下角上。

一、定义XAML外形的样式

1、为了实现这个目的,我们先定义了这样两个Thumb的样式。一个是ResizingStyle样式,这个样式可天生如下的外形

resizingBarrotateBar

响应的样式的代码如下:【加入到App.xaml中】

代码

Style x:Key="ResizingStyle" TargetType="Thumb">

Setter Property="Background" Value="#FF1F3B53"/>

Setter Property="BorderThickness" Value="1"/>

Setter Property="IsTabStop" Value="False"/>

Setter Property="BorderBrush">

Setter.Value>

LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

GradientStop Color="#FFA3AEB9" Offset="0"/>

GradientStop Color="#FF8399A9" Offset="0.375"/>

GradientStop Color="#FF718597" Offset="0.375"/>

GradientStop Color="#FF617584" Offset="1"/>

LinearGradientBrush>

Setter.Value>

Setter>

Setter Property="Template">

Setter.Value>

ControlTemplate TargetType="Thumb">

Grid Width="10" Height="10">

VisualStateManager.VisualStateGroups>

VisualStateGroup x:Name="CommonStates">

VisualState x:Name="Normal"/>

VisualState x:Name="MouseOver">

Storyboard>

DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BackgroundAnimation"/>

ColorAnimation Duration="0" To="#F2FFFFFF" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="BackgroundGradient"/>

ColorAnimation Duration="0" To="#CCFFFFFF" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[2].(GradientStop.Color)" Storyboard.TargetName="BackgroundGradient"/>

ColorAnimation Duration="0" To="#7FFFFFFF" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[3].(GradientStop.Color)" Storyboard.TargetName="BackgroundGradient"/>

Storyboard>

VisualState>

VisualState x:Name="Pressed">

Storyboard>

ColorAnimation Duration="0" To="#FF6DBDD1" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" Storyboard.TargetName="Background"/>

DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BackgroundAnimation"/>

ColorAnimation Duration="0" To="#D8FFFFFF" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="BackgroundGradient"/>

ColorAnimation Duration="0" To="#C6FFFFFF" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="BackgroundGradient"/>

ColorAnimation Duration="0" To="#8CFFFFFF" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[2].(GradientStop.Color)" Storyboard.TargetName="BackgroundGradient"/>

ColorAnimation Duration="0" To="#3FFFFFFF" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[3].(GradientStop.Color)" Storyboard.TargetName="BackgroundGradient"/>

Storyboard>

VisualState>

VisualState x:Name="Disabled">

Storyboard>

DoubleAnimation Duration="0" To=".55" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="DisabledVisualElement"/>

Storyboard>

VisualState>

VisualStateGroup>

VisualStateGroup x:Name="FocusStates">

VisualState x:Name="Focused">

Storyboard>

DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="FocusVisualElement"/>

Storyboard>

VisualState>

VisualState x:Name="Unfocused"/>

VisualStateGroup>

VisualStateManager.VisualStateGroups>

Border x:Name="Background" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="White" >

Grid Background="{TemplateBinding Background}" Margin="1">

Border x:Name="BackgroundAnimation" Background="#FF448DCA" Opacity="0"/>

Rectangle x:Name="BackgroundGradient">

Rectangle.Fill>

LinearGradientBrush EndPoint=".7,1" StartPoint=".7,0">

GradientStop Color="#FFFFFFFF" Offset="0"/>

GradientStop Color="#F9FFFFFF" Offset="0.375"/>

GradientStop Color="#E5FFFFFF" Offset="0.625"/>

GradientStop Color="#C6FFFFFF" Offset="1"/>

LinearGradientBrush>

Rectangle.Fill>

Rectangle>

Path Data="M0,1.3335 L1.3335,0 L1.3335,0.66675001 L3.75,0.66675001 L3.75,2.0002501 L1.3335,2.0002501 L1.3335,2.6670001 z" Fill="#99000000" HorizontalAlignment="Left" Height="2.667" Margin="-0.208,-0.011,0,0" RenderTransformOrigin="0.5,0.5" Stretch="Fill" UseLayoutRounding="False" VerticalAlignment="Top" Width="3.75" >

Path.RenderTransform>

CompositeTransform Rotation="40.311"/>

Path.RenderTransform>

Path>

Path Data="M4.7309999,1.3335 L3.3975,0 L3.3975,0.66675001 L0,0.66675001 L0,2.0002501 L3.3975,2.0002501 L3.3975,2.6670001 z" Fill="#99000000" HorizontalAlignment="Right" Height="2.667" Margin="0,0,2.468,-1.301" RenderTransformOrigin="0.917,0.333" Stretch="Fill" UseLayoutRounding="False" VerticalAlignment="Bottom" Width="4.731">

Path.RenderTransform>

CompositeTransform Rotation="122.849"/>

Path.RenderTransform>

Path>

Grid>

Border>

Rectangle x:Name="DisabledVisualElement" Fill="#FFFFFFFF" IsHitTestVisible="false" Opacity="0" />

Rectangle x:Name="FocusVisualElement" IsHitTestVisible="false" Margin="1" Opacity="0"Stroke="#FF6DBDD1" StrokeThickness="1"/>

Grid>

ControlTemplate>

Setter.Value>

Setter>

Style>

您可能还会对下面的文章感兴趣: