WPFå¨ææ¹ågridè¡å®½æè
åé«ï¼éè¦å建GridLengthçå¨ç»ç±»ã
(ä¸) å建ä¸ä¸ªæ¯æGridLengthç±»åçå¨ç»ç±»
æ°å»ºä¸ä¸ªç»§æ¿AnimationTimeLineçç±»GridLengthAnimation, ç®åå®ç°2个ä¾èµå±æ§"From", "To".代ç å¦ä¸:
internal class GridLengthAnimation : AnimationTimeline
{
static GridLengthAnimation()
{
FromProperty = DependencyProperty.Register("From", typeof(GridLength),
typeof(GridLengthAnimation));
ToProperty = DependencyProperty.Register("To", typeof(GridLength),
typeof(GridLengthAnimation));
}
public static readonly DependencyProperty FromProperty;
public GridLength From
{
get
{
return (GridLength)GetValue(GridLengthAnimation.FromProperty);
}
set
{
SetValue(GridLengthAnimation.FromProperty, value);
}
}
public static readonly DependencyProperty ToProperty;
public GridLength To
{
get
{
return (GridLength)GetValue(GridLengthAnimation.ToProperty);
}
set
{
SetValue(GridLengthAnimation.ToProperty, value);
}
}
æ¥ä¸æ¥å°±æ¥ä¾æ¬¡éè½½æè
å®ç°AnimationTimeLineç±»çæå,
1. éè½½CreateInstanceCore, 代ç å¦ä¸:
protected override System.Windows.Freezable CreateInstanceCore()
{
return new GridLengthAnimation();
}
2. éè½½GetCurrentValue以è¿åå¨ç»çå½åå¼, 代ç å¦ä¸:
public override object GetCurrentValue(object defaultOriginValue,
object defaultDestinationValue, AnimationClock animationClock)
{
double fromVal = ((GridLength)GetValue(GridLengthAnimation.FromProperty)).Value;
double toVal = ((GridLength)GetValue(GridLengthAnimation.ToProperty)).Value;
if (fromVal > toVal)
{
return new GridLength((1 - animationClock.CurrentProgress.Value) * (fromVal - toVal) + toVal,
((GridLength)GetValue(GridLengthAnimation.FromProperty)).GridUnitType);
}
else
return new GridLength(animationClock.CurrentProgress.Value * (toVal - fromVal) + fromVal,
((GridLength)GetValue(GridLengthAnimation.ToProperty)).GridUnitType);
}
3. éåTargetPropertyType å±æ§ä»¥æ示ç¸åºçå¨ç»æçæè¾åºçType, 代ç å¦ä¸:
public override Type TargetPropertyType
{
get
{
return typeof(GridLength);
}
}
ok, éè¿ä¸é¢çæ¥éª¤æ们已ç»å好äºGridLengthAnimationç±», æ¥ä¸æ¥å°±æ¯å¦ä½ä½¿ç¨æ¤ç±».
(äº)xaml使ç¨æ¤ç±», 代ç å¦ä¸:
<Window.Resources>
<Storyboard x:Key="sbDock">
<common:GridLengthAnimation BeginTime="00:00:00" Storyboard.TargetName="_cellLeft" Storyboard.TargetProperty="Width">
</common:GridLengthAnimation>
</Storyboard>
</Window.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="_cellLeft" Width="300"/>
<ColumnDefinition x:Name="_cellRight" Width="*"/>
</Grid.ColumnDefinitions>
</Grid>
(ä¸)c#使ç¨æ¤ç±», 代ç å¦ä¸:
Storyboard sbDock = this.FindResource("sbDock") as Storyboard;
if (sbDock != null)
{
SplineDoubleKeyFrame sdKeyFrame1 = new SplineDoubleKeyFrame(TransformRadius,
KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1)));
(sbDock.Children[0] as DoubleAnimationUsingKeyFrames).KeyFrames.Clear();
(sbDock.Children[0] as DoubleAnimationUsingKeyFrames).KeyFrames.Add(sdKeyFrame1);
(sbDock.Children[1] as GridLengthAnimation).From = new GridLength(300, GridUnitType.Pixel);
(sbDock.Children[1] as GridLengthAnimation).To = new GridLength(0, GridUnitType.Pixel);
sbDock.Begin();
}
温馨提示:内容为网友见解,仅供参考