日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

[答网友问]让GridLength支持动画

發(fā)布時(shí)間:2024/4/14 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [答网友问]让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);
????????????}

????????}

??????

????}

}



下載Demo


?

總結(jié)

以上是生活随笔為你收集整理的[答网友问]让GridLength支持动画的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。