我有一个在我的xaml中定义的边界。我需要以编程方式将另一个控件维度设置为与我的xaml中定义的边框相同的控件维度。
我似乎无法得到尺寸,因为高度和宽度被设置为自动和水平对齐和垂直对齐设置为拉伸。
<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网格的尺寸,但是这个网格的高度和宽度也是自动的。
有什么方法可以让我在不定义固定高度和宽度的情况下获得这个控件的维数?
发布于 2014-01-17 14:32:52
使用LayoutUpdated事件计算所有值。为前任
void MainPage_LayoutUpdated(object sender, EventArgs e)
{
borderPlaceHolderIframe.Width
borderPlaceHolderIframe.ActualWidth
borderPlaceHolderIframe.DesiredSize
borderPlaceHolderIframe.RenderSize
}发布于 2014-01-17 14:33:40
结果显示,维度获取/设置了Framework元素的维度。无法保证何时计算这些值。
为了解决这个问题,我调用了附加在处理程序上的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事件也可以获得相同的结果,代码是-
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 Subhttps://stackoverflow.com/questions/21187554
复制相似问题