且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

测量在WPF中在运行时创建的控件

更新时间:2023-12-06 15:10:28

解决了!

问题出在我的XAML中.在标签模板的最高层,有一个父画布,没有高度或宽度字段.由于不必修改其子级的大小,因此将其始终设置为0,0.通过删除该节点并用必须调整其大小以适合其子节点的边框替换根节点,可以更新height和width字段,并在Measure()调用中传播回我的代码.

The issue was in my XAML. At the highest level of my label's template there was a parent canvas that had no height or width field. Since this did not have to modify its size for its children it was constantly set to 0,0. By removing it and replacing the root node with a border, which must resize to fit its children, the height and width fields are updated and propagated back to my code on Measure() calls.