为什么在这种情况下ActualWidth和ActualHeight 0.0?

编程入门 行业动态 更新时间:2024-10-28 12:29:24
本文介绍了为什么在这种情况下ActualWidth和ActualHeight 0.0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在 Canvas 内定义了一个 Grid >

< Canvas x:Name =outerCanvas> < Grid Grid.Row =1Name =cGridShowGridLines =TrueWidth ={Binding Path = ActualWidth,RelativeSource = {RelativeSource AncestorType = {x:Type Canvas}}}Height = {Binding Path = ActualHeight,RelativeSource = {RelativeSource AncestorType = {x:Type Canvas}}}> < Grid.ColumnDefinitions> < ColumnDefinition /> < ColumnDefinition /> < ColumnDefinition /> < /Grid.ColumnDefinitions> < Grid.RowDefinitions> < RowDefinition /> < RowDefinition /> < /Grid.RowDefinitions> < Rectangle Name =rectangle1Stroke =BlackFill =AntiqueWhite/> 我的问题是,在Window构造函数中,在 InitializeComponents() Grid.ColumnDefinitions [0] .ActualWidth 或任意矩形。 ActualWidth 全是设置为0.0(高度相同)。我不知道该怎么做才能获得这些信息。任何帮助?

观察:

  • 我没有定义外部画布宽度和高度,但如果我这样做,它不能解决我的问题。
  • 在运行时我可以看到这 Canvas / Grid 占据整个窗口空间,所以它内部的每个矩形都有 ActualWidth s和 ActualHeight s
  • 网格的宽度/高度绑定到画布上,但我尝试删除此绑定,并且我的问题仍然存在。 解决方案 c $ c> ActualHeight 和 ActualWidth 在测量和排列控件之前不会被设置。通常在 InitializeComponent()中没有任何内容会导致一个度量,所以当它返回时它们仍然是零。

    您可以在之前手动调用窗口的 Measure()和 Arrange()方法来强制计算这些值窗口的 InitializeComponent()返回。

    如果您调整内容大小:

    window.Measure(new Size(double.PositiveInfinity,double.PositiveInfinity)); window.Arrange(new Rect(0,0,window.DesiredSize.Width,window.DesiredSize.Height));

    如果您使用明确的窗口大小:

    window.Measure(new Size(Width,Height)); window.Arrange(new Rect(0,0,window.DesiredSize.Width,window.DesiredSize.Height));

    I have a Grid inside a Canvas defined like this:

    <Canvas x:Name="outerCanvas"> <Grid Grid.Row="1" Name="cGrid" ShowGridLines="True" Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type Canvas}}}" Height="{Binding Path=ActualHeight, RelativeSource={RelativeSource AncestorType={x:Type Canvas}}}"> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <Rectangle Name="rectangle1" Stroke="Black" Fill="AntiqueWhite" /> <Rectangle Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="1" Grid.RowSpan="1" Name="rectangle2" Stroke="Black" Fill="AliceBlue" /> <Rectangle Grid.Row="0" Grid.Column="2" Grid.ColumnSpan="1" Grid.RowSpan="1" Name="rectangle3" Stroke="Black" Fill="Aqua" /> <Rectangle Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" Grid.RowSpan="1" Name="rectangle4" Stroke="Black" Fill="DarkViolet" /> </Grid> </Canvas>

    My problem is that, on the Window constructor, after InitializeComponents() either Grid.ColumnDefinitions[0].ActualWidth or "any rectangle".ActualWidth are all set to 0.0 (the same for heights). I'm not figuring out what to do to get this information. Any help?

    Observations:

  • I'm not defining the outer canvas width and height but if I do, it doesn't solve my problem.
  • At runtime I can see that this Canvas/Grid occupies the entire window space, so every rectangle inside it has ActualWidths and ActualHeights
  • The grid's width/height is bound to the canvas but I tried removing this binding and my problem still persists.
  • 解决方案

    ActualHeight and ActualWidth are not set until the control is measured and arranged. Usually there is nothing in InitializeComponent() that causes a measure, so when it returns these will still be zero.

    You can force these to be computed earlier by simply calling the window's Measure() and Arrange() methods manually after the window's InitializeComponent() returns.

    If you are sizing to content:

    window.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); window.Arrange(new Rect(0, 0, window.DesiredSize.Width, window.DesiredSize.Height));

    If you are using an explicit window size:

    window.Measure(new Size(Width, Height)); window.Arrange(new Rect(0, 0, window.DesiredSize.Width, window.DesiredSize.Height));

    更多推荐

    为什么在这种情况下ActualWidth和ActualHeight 0.0?

    本文发布于:2023-08-04 02:25:11,感谢您对本站的认可!
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:在这种情况下   ActualWidth   ActualHeight

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!