0

I have a border which is defined in my xaml. I need to programmatically set another controls dimensions to the same as the border defined in my xaml.

I can't seem to get the dimensions as the Height & width are set to auto and Horizontal Alignment & Vertical Alignment are set to stretch.

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

I have tried

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

I also tried getting the dimensions of the layoutRoot grid which the border is placed in, however the height & width of this are also Auto.

Is there any way of me getting the dimensions of this control without defining a fixed height & width?

DNKROZ
  • 2,634
  • 4
  • 25
  • 43
  • See this post: http://stackoverflow.com/questions/1602148/binding-to-actualwidth-does-not-work – Rob J Jan 18 '14 at 15:29

2 Answers2

1

Use LayoutUpdated event for calculating all the value. for ex

  void MainPage_LayoutUpdated(object sender, EventArgs e)
    {
    borderPlaceHolderIframe.Width 
    borderPlaceHolderIframe.ActualWidth  
    borderPlaceHolderIframe.DesiredSize 
    borderPlaceHolderIframe.RenderSize
    }
Heena
  • 8,450
  • 1
  • 22
  • 40
0

It turns out that the dimensions get/set the dimensions of a Framework element. There is no guarantee when the values will be calculated.

To get around this issue i called beginInvoke attached to a handler. In this method i could then access the values i needed. (You can only access the values in this method alone, so i suggest storing the values into global variables if you wish to use them elsewhere)

This is the code i used -

//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

For reference i believe that the same result can also be achived by using a sizeChanged event, the code is -

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
DNKROZ
  • 2,634
  • 4
  • 25
  • 43