gpt4 book ai didi

wpf - WPF控件的实际大小

转载 作者:行者123 更新时间:2023-12-04 14:44:14 25 4
gpt4 key购买 nike

我有这段XAML代码:

<Window x:Class="SizingTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<Label x:Name="theLabel" Width="Auto">A very large label with a lot of text</Label>
</Grid>
</Window>

我想在后面的代码中,我试图获取标签的实际宽度
theLabel.ActualWidth

可以解决问题,但是尝试以下代码后:
public Window1()
{
InitializeComponent();
double width = theLabel.ActualWidth;
}

width的值为0,我还检查了返回NaN的theLabel.Width,也返回了0的theLabel.DesiredSize.Width。我可以用来查找标签的实际宽度吗?

谢谢你。

最佳答案

在布置组件的父项(和可能的子项)之前,不设置ActualWidth

要获取组件的ActualWidth,您需要等待布局传递完成。收听Loaded事件,直到第一次布局通过后才调用它。

关于wpf - WPF控件的实际大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1134999/

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