2012-08-09 44 views
3

我試圖對事件採取行動創建一個對象,可重複使用的對象的數量。 要做到這一點,我在存儲標記屬性的目標對象的名稱。 所以在此事件觸發:使用標記屬性來定位C#

private void ShowDeleteButton(object sender, System.Windows.Input.MouseEventArgs e) 
{ 
    Duration TimeToTake = new Duration(new TimeSpan(0,0,0,0,300)); 
    DoubleAnimation ShowButton = new DoubleAnimation(0, 104, TimeToTake); 
    DoubleAnimation HideButton = new DoubleAnimation(104, 0, TimeToTake); 
    (sender as Rectangle).Tag.BeginAnimation(Button.WidthProperty, ShowButton); 
} 

顯然使用(發件人爲按鈕).TAG作爲名稱的對象將不能正常工作。那麼如何將標籤屬性轉換爲目標對象的引用呢?

注意這是WPF

感謝

回答

1

對於WPF,使用FindName

var oControl = this.FindName((string)(sender as Button).Tag); 
if (oControl != null) 
{ 
    (Rectangle)oControl.BeginAnimation... 
} 

有關的WinForms,你可以找到存儲在標籤上的名稱對照:

var aoControls = this.Controls.Find((string)(sender as Button).Tag, true); 
if ((aoControls != null) && (aoControls.Length != 0)) 
{ 
    (Rectangle)aoControls[0].BeginAnimation... 
} 
0

你指的UIElement.BeginAnimation和發送者可以是任何UIElement?如果是這樣,那麼你可以簡單地做:

((UiElement)sender).BeginAnimation(Button.WidthProperty, ShowButton); 
相關問題