簡體   English   中英

按鈕縮放邊界(條件)

[英]Button scaled boundries(conditions)

大家,我試圖縮放按鈕的大小,代碼工作正常,如果我只單擊一次,但是如果我連續多次單擊按鈕,按鈕無法返回其原始大小。 這是代碼:

 private void ButtonSearchMedication_OnClick(object sender, RoutedEventArgs e)
    {


            //Assign variation of width in term of begin, end and duration
        DoubleAnimation widthAnimation  =new DoubleAnimation(ButtonSearchMedication.ActualWidth, ButtonSearchMedication.ActualWidth*0.8, new Duration(timeSpan:TimeSpan.FromSeconds(0.2)) );

        //Assign variation of height in term of begin, end and duration
        DoubleAnimation heightAnimation = new DoubleAnimation(ButtonSearchMedication.ActualHeight,ButtonSearchMedication.ActualHeight*0.8, new Duration(timeSpan:TimeSpan.FromSeconds(0.2)));

        //Assign properties to button
        ButtonSearchMedication.BeginAnimation(Button.WidthProperty,widthAnimation);
        ButtonSearchMedication.BeginAnimation(Button.HeightProperty,heightAnimation);
    }

私有無效ButtonSearchMedication_OnMouseLeave(對象發送者,MouseEventArgs e){

            DoubleAnimation widthAnimation = new DoubleAnimation(ButtonSearchMedication.ActualWidth, ButtonSearchMedication.ActualWidth*1.25,new Duration(timeSpan:TimeSpan.FromSeconds(0.2)));
            DoubleAnimation heightAnimation = new DoubleAnimation(ButtonSearchMedication.ActualHeight, ButtonSearchMedication.ActualHeight*1.25,new Duration(timeSpan:TimeSpan.FromSeconds(0.2)));
            ButtonSearchMedication.BeginAnimation(Button.WidthProperty,widthAnimation);
            ButtonSearchMedication.BeginAnimation(Button.HeightProperty,heightAnimation);
        }

我能做些什么來確保每次 MouseLeave 后按鈕的大小都變成原來的大小? 謝謝

鼠標點擊/鼠標離開並不真正匹配。 您可以不點擊就離開。

無論如何,在這里給你一個開始是固定代碼:

const double _width = 200;
const double _height = 100;

void ButtonSearchMedication_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    var widthAnimation = new DoubleAnimation(_width * 0.8, TimeSpan.FromSeconds(0.2));
    var heightAnimation = new DoubleAnimation(_height * 0.8, TimeSpan.FromSeconds(0.2));
    buttonSearchMedication.BeginAnimation(WidthProperty, widthAnimation);
    buttonSearchMedication.BeginAnimation(HeightProperty, heightAnimation);
}

void ButtonSearchMedication_PreviewMouseLeftButtonUp(object sender, MouseEventArgs e)
{
    var widthAnimation = new DoubleAnimation(_width, TimeSpan.FromSeconds(0.2));
    var heightAnimation = new DoubleAnimation(_height, TimeSpan.FromSeconds(0.2));
    buttonSearchMedication.BeginAnimation(WidthProperty, widthAnimation);
    buttonSearchMedication.BeginAnimation(HeightProperty, heightAnimation);
}

我決定使用鼠標向下/向上事件,您可以將其更改為進入/離開或其他。

如您所見,大小是恆定的,動畫僅to參數(這種方式在單擊事件的情況下不會堆疊多個動作)。 如果按鈕大小是動態的,那么您必須在開始動畫之前檢索和存儲原始大小,可能通過使用另一個事件(例如鼠標輸入)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM