[答网友问]让GridLength支持动画
生活随笔
收集整理的這篇文章主要介紹了
[答网友问]让GridLength支持动画
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
[答網友問]WPF中讓GridLength類型支持動畫
?????????????????????????????????????????????????????????????? 周銀輝
今天一位收到網友求助:
"你好!我想向你請教關于動畫的問題:比如,一個Page分為上、下l兩塊,我想通過動畫實現分別隱藏某塊的內容,當我觸發一個按鈕的Click事件時,實現隱藏上面的面板,同時下的面板就要延伸并占據上面面板的空間,這個過程到好實現,問題是我如果第二次觸發這個按鈕的Click事件時,怎么才能讓上面的面板出現(恢復原始大小)并且下面的面板的大小也回到原始大小"
對于這個問題,有一個很好的解決方法是,將兩個面板放到Grid中,并讓GridLength類型支持動畫,就像Double類型有著對應的DoubleAnimation一樣.這樣就將網友的問題轉化為:上面的面板所在行對應RowDefinition的高度由0.5變為0,再由0變為0.5(單位GridUnitType.Star)
以下是GridLengthAnimation類的完整代碼,你可以使用她就像使用DoubleAnimaion一樣.
using?System;
using?System.Collections.Generic;
using?System.Text;
using?System.Windows.Media.Animation;
using?System.Windows;
using?System.Diagnostics;
namespace?GridAnimationDemo
{
????internal?class?GridLengthAnimation?:?AnimationTimeline
????{
????????
????????public?static?readonly?DependencyProperty?FromProperty;
????????public?static?readonly?DependencyProperty?ToProperty;
???????
????????static?GridLengthAnimation()
????????{
????????????FromProperty?=?DependencyProperty.Register("From",?typeof(GridLength),
????????????????typeof(GridLengthAnimation));
????????????ToProperty?=?DependencyProperty.Register("To",?typeof(GridLength),?
????????????????typeof(GridLengthAnimation));
????????}
???????
????????public?override?Type?TargetPropertyType
????????{
????????????get?
????????????{
????????????????return?typeof(GridLength);
????????????}
????????}
????????protected?override?System.Windows.Freezable?CreateInstanceCore()
????????{
????????????return?new?GridLengthAnimation();
????????}
????????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,?GridUnitType.Star);
????????????}
????????????else
????????????????return?new?GridLength(animationClock.CurrentProgress.Value?*?(toVal?-?fromVal)?+?fromVal,?GridUnitType.Star);
????????}
??????
????????
????????public?GridLength?From
????????{
????????????get
????????????{
????????????????return?(GridLength)GetValue(GridLengthAnimation.FromProperty);
????????????}
????????????set
????????????{
????????????????SetValue(GridLengthAnimation.FromProperty,?value);
????????????}
????????}
????????public?GridLength?To
????????{
????????????get
????????????{
????????????????return?(GridLength)GetValue(GridLengthAnimation.ToProperty);
????????????}
????????????set
????????????{
????????????????SetValue(GridLengthAnimation.ToProperty,?value);
????????????}
????????}
??????
????}
}
?????????????????????????????????????????????????????????????? 周銀輝
今天一位收到網友求助:
"你好!我想向你請教關于動畫的問題:比如,一個Page分為上、下l兩塊,我想通過動畫實現分別隱藏某塊的內容,當我觸發一個按鈕的Click事件時,實現隱藏上面的面板,同時下的面板就要延伸并占據上面面板的空間,這個過程到好實現,問題是我如果第二次觸發這個按鈕的Click事件時,怎么才能讓上面的面板出現(恢復原始大小)并且下面的面板的大小也回到原始大小"
對于這個問題,有一個很好的解決方法是,將兩個面板放到Grid中,并讓GridLength類型支持動畫,就像Double類型有著對應的DoubleAnimation一樣.這樣就將網友的問題轉化為:上面的面板所在行對應RowDefinition的高度由0.5變為0,再由0變為0.5(單位GridUnitType.Star)
以下是GridLengthAnimation類的完整代碼,你可以使用她就像使用DoubleAnimaion一樣.
using?System;
using?System.Collections.Generic;
using?System.Text;
using?System.Windows.Media.Animation;
using?System.Windows;
using?System.Diagnostics;
namespace?GridAnimationDemo
{
????internal?class?GridLengthAnimation?:?AnimationTimeline
????{
????????
????????public?static?readonly?DependencyProperty?FromProperty;
????????public?static?readonly?DependencyProperty?ToProperty;
???????
????????static?GridLengthAnimation()
????????{
????????????FromProperty?=?DependencyProperty.Register("From",?typeof(GridLength),
????????????????typeof(GridLengthAnimation));
????????????ToProperty?=?DependencyProperty.Register("To",?typeof(GridLength),?
????????????????typeof(GridLengthAnimation));
????????}
???????
????????public?override?Type?TargetPropertyType
????????{
????????????get?
????????????{
????????????????return?typeof(GridLength);
????????????}
????????}
????????protected?override?System.Windows.Freezable?CreateInstanceCore()
????????{
????????????return?new?GridLengthAnimation();
????????}
????????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,?GridUnitType.Star);
????????????}
????????????else
????????????????return?new?GridLength(animationClock.CurrentProgress.Value?*?(toVal?-?fromVal)?+?fromVal,?GridUnitType.Star);
????????}
??????
????????
????????public?GridLength?From
????????{
????????????get
????????????{
????????????????return?(GridLength)GetValue(GridLengthAnimation.FromProperty);
????????????}
????????????set
????????????{
????????????????SetValue(GridLengthAnimation.FromProperty,?value);
????????????}
????????}
????????public?GridLength?To
????????{
????????????get
????????????{
????????????????return?(GridLength)GetValue(GridLengthAnimation.ToProperty);
????????????}
????????????set
????????????{
????????????????SetValue(GridLengthAnimation.ToProperty,?value);
????????????}
????????}
??????
????}
}
下載Demo
?
總結
以上是生活随笔為你收集整理的[答网友问]让GridLength支持动画的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 硬件常识
- 下一篇: 关闭弹出窗口刷新父窗口