[答网友问]让GridLength支持动画
生活随笔
收集整理的這篇文章主要介紹了
[答网友问]让GridLength支持动画
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
[答網(wǎng)友問(wèn)]WPF中讓GridLength類(lèi)型支持動(dòng)畫(huà)
?????????????????????????????????????????????????????????????? 周銀輝
今天一位收到網(wǎng)友求助:
"你好!我想向你請(qǐng)教關(guān)于動(dòng)畫(huà)的問(wèn)題:比如,一個(gè)Page分為上、下l兩塊,我想通過(guò)動(dòng)畫(huà)實(shí)現(xiàn)分別隱藏某塊的內(nèi)容,當(dāng)我觸發(fā)一個(gè)按鈕的Click事件時(shí),實(shí)現(xiàn)隱藏上面的面板,同時(shí)下的面板就要延伸并占據(jù)上面面板的空間,這個(gè)過(guò)程到好實(shí)現(xiàn),問(wèn)題是我如果第二次觸發(fā)這個(gè)按鈕的Click事件時(shí),怎么才能讓上面的面板出現(xiàn)(恢復(fù)原始大小)并且下面的面板的大小也回到原始大小"
對(duì)于這個(gè)問(wèn)題,有一個(gè)很好的解決方法是,將兩個(gè)面板放到Grid中,并讓GridLength類(lèi)型支持動(dòng)畫(huà),就像Double類(lèi)型有著對(duì)應(yīng)的DoubleAnimation一樣.這樣就將網(wǎng)友的問(wèn)題轉(zhuǎn)化為:上面的面板所在行對(duì)應(yīng)RowDefinition的高度由0.5變?yōu)?,再由0變?yōu)?.5(單位GridUnitType.Star)
以下是GridLengthAnimation類(lèi)的完整代碼,你可以使用她就像使用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);
????????????}
????????}
??????
????}
}
?????????????????????????????????????????????????????????????? 周銀輝
今天一位收到網(wǎng)友求助:
"你好!我想向你請(qǐng)教關(guān)于動(dòng)畫(huà)的問(wèn)題:比如,一個(gè)Page分為上、下l兩塊,我想通過(guò)動(dòng)畫(huà)實(shí)現(xiàn)分別隱藏某塊的內(nèi)容,當(dāng)我觸發(fā)一個(gè)按鈕的Click事件時(shí),實(shí)現(xiàn)隱藏上面的面板,同時(shí)下的面板就要延伸并占據(jù)上面面板的空間,這個(gè)過(guò)程到好實(shí)現(xiàn),問(wèn)題是我如果第二次觸發(fā)這個(gè)按鈕的Click事件時(shí),怎么才能讓上面的面板出現(xiàn)(恢復(fù)原始大小)并且下面的面板的大小也回到原始大小"
對(duì)于這個(gè)問(wèn)題,有一個(gè)很好的解決方法是,將兩個(gè)面板放到Grid中,并讓GridLength類(lèi)型支持動(dòng)畫(huà),就像Double類(lèi)型有著對(duì)應(yīng)的DoubleAnimation一樣.這樣就將網(wǎng)友的問(wèn)題轉(zhuǎn)化為:上面的面板所在行對(duì)應(yīng)RowDefinition的高度由0.5變?yōu)?,再由0變?yōu)?.5(單位GridUnitType.Star)
以下是GridLengthAnimation類(lèi)的完整代碼,你可以使用她就像使用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
?
總結(jié)
以上是生活随笔為你收集整理的[答网友问]让GridLength支持动画的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 硬件常识
- 下一篇: 关闭弹出窗口刷新父窗口