在渲染之前获取 UserControl 的实际大小 [英] Getting Actual Size of UserControl before rendering

查看:15
本文介绍了在渲染之前获取 UserControl 的实际大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 UserControl 类的 Measure 方法来呈现 MyUserControl 的 ActualSize,正如我为我的 上一个问题.但是,它不起作用.MyUserControl 有一个 ItemsControl,它与一个 List 进行数据绑定,如下所示.通过 uc.MyCollection = myCollection; 添加的项目没有反映在 uc.DesiredSize.Height 中.

I am trying to get the ActualSize of MyUserControl before it gets rendered using Measure methode of UserControl class as suggested for my previous question. However it is not working. MyUserControl has an ItemsControl that is databound with a List as shown below. The items added through uc.MyCollection = myCollection; is not getting reflected in uc.DesiredSize.Height.

MyUserControl uc= new MyUserControl();
uc.AName = "a1";
uc.Width = 194;
uc.MyCollection = myCollection; //myCollection is  a List databound to ItemsControl inside MyUserControl
uc.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
uc.Arrange(new Rect(0, 0, uc.DesiredSize.Width, uc.DesiredSize.Width)); //Needed?
uc.UCHeight = uc.DesiredSize.Height; //size of items databound to ItemsControl not reflected here
uc.UCWidth = uc.DesiredSize.Width;
uc.Margin = new Thickness(34, 42, 0, 0);
myRoot.Children.Add(uc); //myRoot is Canvas

推荐答案

你必须在你的 uc 中重写 MeasureOverride 和ArrangeOverride 来计算你自己的大小.在 MeasureOverride 内部询问"您的列表,并使用其自身大小的度量" - 这样您就可以计算自己的 uc 的大小(在 MeasureOverride 中返回此大小)-然后在调用 uc.Measure 后获得 DesiredSize.

You have to override MeasureOverride and ArrangeOverride in your uc to calculate the size for your own. Inside MeasureOverride "ask" your List with "Measure" of its own size - so you can calculate the size for your own uc (returning this size in MeasureOverride) - then you get your DesiredSize after calling uc.Measure.

这篇关于在渲染之前获取 UserControl 的实际大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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