Silverlight带关闭动画的内容控件,可移动的内容控件(一)
本例給大家介紹兩個(gè)自定義控件,一個(gè)有顯示和關(guān)閉兩種狀態(tài),在狀態(tài)切換時(shí)有動(dòng)畫效果。另外一個(gè)是可以拖動(dòng)的內(nèi)容控件,可以制作能拖動(dòng)的面板。
???????? A.帶關(guān)閉動(dòng)畫的內(nèi)容控件。
.xaml
View Code <ResourceDictionaryxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SuperMapStandardMapApp1">
<Style TargetType="local:CustomPanel">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:CustomPanel">
<Grid x:Name="LayoutRoot" RenderTransformOrigin="{TemplateBinding RenderTransformOrigin}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="ViewStates">
<VisualState x:Name="Open">
<Storyboard>
<DoubleAnimation BeginTime="00:00:00" Duration="00:00:0.3" Storyboard.TargetName="CustomPanelScale" Storyboard.TargetProperty="ScaleY" To="1" />
<DoubleAnimation BeginTime="00:00:00" Duration="00:00:0.3" Storyboard.TargetName="CustomPanelScale" Storyboard.TargetProperty="ScaleX" To="1" />
<DoubleAnimation BeginTime="00:00:00" Duration="00:00:0.3" Storyboard.TargetName="LayoutRoot" Storyboard.TargetProperty="Opacity" To="1" />
</Storyboard>
</VisualState>
<VisualState x:Name="Close">
<Storyboard>
<DoubleAnimation BeginTime="00:00:00" Duration="00:00:0.2" Storyboard.TargetName="LayoutRoot" Storyboard.TargetProperty="Opacity" To="0" />
<DoubleAnimation BeginTime="00:00:00" Duration="00:00:0.2" Storyboard.TargetName="CustomPanelScale" Storyboard.TargetProperty="ScaleY" To="0" />
<DoubleAnimation BeginTime="00:00:00" Duration="00:00:0.2" Storyboard.TargetName="CustomPanelScale" Storyboard.TargetProperty="ScaleX" To="1" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentPresenter x:Name="Content"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}">
</ContentPresenter>
<Grid.RenderTransform>
<ScaleTransform x:Name="CustomPanelScale" ScaleX="1" ScaleY="1" />
</Grid.RenderTransform>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="Red"/>
</Style>
</ResourceDictionary>
定義兩 VisualState 一個(gè)open,一個(gè)close 代表內(nèi)容控件的兩個(gè)狀態(tài),添加ContentPresenter標(biāo)簽代表內(nèi)容控件所添加的內(nèi)容。
.cs
View Code namespace SuperMapStandardMapApp1{
[TemplateVisualState(GroupName = "ViewStates", Name = "Open")]
[TemplateVisualState(GroupName = "ViewStates", Name = "Close")]
public partial class CustomPanel : ContentControl
{
public CustomPanel()
{
DefaultStyleKey = typeof(CustomPanel);
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
this.ChangeVisualState(true);
}
#region Dependency Properties
/// <summary>
/// 獲取或設(shè)置內(nèi)容控件是否顯示
/// </summary>
/// <value>
/// <c>true</c> 設(shè)置時(shí)控件進(jìn)行顯示; 否則不顯示, <c>false</c>.
/// </value>
///
public bool IsOpnen
{
get { return (bool)GetValue(IsOpenProperty); }
set { SetValue(IsOpenProperty, value); }
}
public static readonly DependencyProperty IsOpenProperty = DependencyProperty.Register("IsOpen", typeof(bool), typeof(CustomPanel), new PropertyMetadata(true, OnIsOpenPertyChange));
public static void OnIsOpenPertyChange(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
(obj as CustomPanel).ChangeVisualState(true);
}
#endregion
private void ChangeVisualState(bool useTransitions)
{
if (IsOpnen)
{
VisualStateManager.GoToState(this, "Open", useTransitions);
}
else
{
VisualStateManager.GoToState(this, "Close", useTransitions);
}
}
}
}
說(shuō)明兩個(gè):TemplateVisualState分別代表close和open狀態(tài),注冊(cè)一個(gè)DependencyProperty ISOpen表示此內(nèi)容面板是否開啟,在ChangeVisualState方法中,通過(guò) VisualStateManager.GoToState(this, "Open", useTransitions); VisualStateManager.GoToState(this, "Close", useTransitions);說(shuō)明轉(zhuǎn)化到哪個(gè)狀態(tài)。這樣便定義了一個(gè)可以有兩狀態(tài)相互轉(zhuǎn)化的內(nèi)容控件!如圖:
轉(zhuǎn)載于:https://www.cnblogs.com/doudougou/archive/2011/08/14/2138491.html
總結(jié)
以上是生活随笔為你收集整理的Silverlight带关闭动画的内容控件,可移动的内容控件(一)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【过关斩将】如何制作高水平简历-原则篇
- 下一篇: 人脸重建速览,从3DMM到表情驱动动画