为什么在这种情况下ActualWidth和ActualHeight 0.0? [英] Why are ActualWidth and ActualHeight 0.0 in this case?

查看:284
本文介绍了为什么在这种情况下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/>
< Rectangle Grid.Row =0Grid.Column =2Grid.ColumnSpan =1Grid.RowSpan =1Name =rectangle3Stroke =BlackFill =Aqua />
< / Grid>
< / Canvas>

我的问题是,在Window构造函数中,在 InitializeComponents() Grid.ColumnDefinitions [0] .ActualWidth 或任意矩形。 ActualWidth 全是设置为0.0(高度相同)。我不知道该怎么做才能获得这些信息。任何帮助?



观察:


  1. 我没有定义外部画布宽度和高度,但如果我这样做,它不能解决我的问题。
  2. 在运行时我可以看到这 Canvas / Grid 占据整个窗口空间,所以它内部的每个矩形都有 ActualWidth s和 ActualHeight s

  3. 网格的宽度/高度绑定到画布上,但我尝试删除此绑定,并且我的问题仍然存在。 解决方案 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:

    1. I'm not defining the outer canvas width and height but if I do, it doesn't solve my problem.
    2. At runtime I can see that this Canvas/Grid occupies the entire window space, so every rectangle inside it has ActualWidths and ActualHeights
    3. 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆