2014-01-17 24 views
0

我有一個在我的xaml中定義的邊框。我需要以編程方式將另一個控件尺寸設置爲與我在xaml中定義的邊框相同。Silverlight - 獲取設置爲拉伸/自動的控件的尺寸

我似乎無法獲得尺寸,因爲高度&寬度設置爲自動並且水平對齊&垂直對齊設置爲伸展。

<Border BorderBrush="Silver" BorderThickness="1" Height="Auto" Width="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Name="borderPlaceHolderIframe" /> 

我已經試過

borderPlaceHolderIframe.Width //(Result= -1.#IND) 
borderPlaceHolderIframe.ActualWidth //(Result= 0.0) 
borderPlaceHolderIframe.DesiredSize //(Result= 0.0) 
borderPlaceHolderIframe.RenderSize //(Result= 0.0) 

我也試圖讓在邊界被放置在layoutRoot網格的尺寸,不過,這樣的高度&寬度也是汽車。

有沒有什麼辦法讓我獲得這個控件的尺寸,而沒有定義一個固定的高度&寬度?

+0

看到這個職位:http://stackoverflow.com/questions/1602148/binding-to-actualwidth-does-not工作 –

回答

1

使用LayoutUpdated事件計算所有的值。對於例如

void MainPage_LayoutUpdated(object sender, EventArgs e) 
    { 
    borderPlaceHolderIframe.Width 
    borderPlaceHolderIframe.ActualWidth 
    borderPlaceHolderIframe.DesiredSize 
    borderPlaceHolderIframe.RenderSize 
    } 
+0

Brilliant.I排序的問題,但是你的方式更簡單,並且j OB!謝謝 – DNKROZ

+0

謝謝你的申請.. –

0

事實證明,尺寸獲取/設置框架元素的尺寸。計算值時不能保證。

爲了解決此問題,我將beginInvoke附加到處理程序。在這種方法中,我可以訪問我需要的值。 (您只能訪問值僅在此方法中,所以我建議將值存儲爲全局變量,如果你想在其他地方使用它們)

這是我使用的代碼 -

//ActualWidth and ActualHeight are calculated values and may not be set yet 
//therefore, execute GetLayoutRootActualSize() asynchronously on the thread the Dispatcher is associated with 

Me.Dispatcher.BeginInvoke(AddressOf GetLayoutRootActualSize) 

Private Sub GetLayoutRootActualSize() 
    Me.tbxInvoke.Text = Me.LayoutRoot.ActualWidth.ToString() & ", " & Me.LayoutRoot.ActualHeight.ToString() 
End Sub 

僅供參考我相信同樣的結果也可以通過使用SizeChanged事件來achived,代碼 -

Private Sub LayoutRoot_SizeChanged(ByVal sender As Object, ByVal e As System.Windows.SizeChangedEventArgs) Handles LayoutRoot.SizeChanged 
    Me.tbxSizeChanged.Text = Me.LayoutRoot.ActualWidth.ToString() & ", " & Me.LayoutRoot.ActualHeight.ToString() 
End Sub