gpt4 book ai didi

c# - 以编程方式获取当前列的宽度和高度?

转载 作者:行者123 更新时间:2023-12-02 05:32:30 25 4
gpt4 key购买 nike

我试图在 UserControl 中以编程方式获取网格的宽度和高度。

这是我的 XAML 网格:

<Grid Name="BallGrid" Background="DarkBlue" Height="auto" Width="auto">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
</Grid>

但是这两个:

double width = BallGrid.ColumnDefinitions[0].ActualWidth;
double height = BallGrid.RowDefinitions[0].ActualHeight;

返回0,

还有这些

double width = BallGrid.ColumnDefinitions[0].Width.Value;
double height = BallGrid.RowDefinitions[0].Height.Value;

都返回1.

我想知道实际宽度,最好使用与页边距大小相同的单位。如何以编程方式获取这些值?

列值在 ProgessChanged 方法中查询,该方法由 BackgroundWorker 每 50 毫秒调用一次,也许这会有所不同。

最佳答案

通常你使用ActualWidth/Height,这里这些属性有点懒:

When you add or remove rows or columns, the ActualWidth for all ColumnDefinition elements and the ActualHeight of all RowDefinition elements becomes zero until Measure is called.

因此您可以手动调用 Measure 或尝试等待它。

关于c# - 以编程方式获取当前列的宽度和高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12200838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com