2009-04-11 39 views
0

我試過Google,但可能我沒有形成我的查詢權,'因爲我收到了很多廢話和無關緊要的問題。可能太簡單了一個問題。抱歉,自從我上次做Silverlight以來已經有一段時間了!從JavaScript獲取XAML UI元素

如果我有一個XAML元素:

<Image Canvas.Top="128" x:Name="img4_7" Width="180.3" Height="117" Source="monkey1.jpg" Stretch="Fill" Canvas.Left="-3.344" Opacity="0.595"/> 

我怎樣才能像在JavaScript中使用x:名稱?我正在尋找諸如getElementById()之類的東西,但對於Silverlight。我幾乎想要這樣的事情:

img4_7=rootElement.children.getItem('img4_7'); 

很明顯,這是行不通的!

我沒有使用ASP.NET AJAX。

感謝您爲這個noob問題的任何幫助!

回答

1

想通了。

需要使用control.content.findName()

這裏是我的handLoad,爲他人受益:

handleLoad: function(control, userContext, rootElement) 
{ 
    this.control = control; 

    // Sample event hookup: 
    rootElement.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown)); 

    this.img4_7=control.content.findName("img4_7"); 

    if (this.img4_7) { 
     this.img4_7.addEventListener("MouseEnter", Silverlight.createDelegate(this,this.handleMouseEnter)); 
     this.img4_7.addEventListener("MouseLeave",Silverlight.createDelegate(this,this.handleMouseLeave)); 

    } 


}, 
+0

原諒我撞到這個老問題。但我的情況與你完全一樣,但它不起作用... 我需要此元素: 其中x:Name是唯一名稱(每個start_data_letter都有一個名稱) – Matthias 2011-05-07 19:53:49