c# - 渲染前获取 UserControl 的实际大小

标签 c# silverlight

我正在尝试获取 MyUserControl 的 ActualSize,然后使用 UserControl 类的 Measure 方法呈现它,如我的 previous question 所建议的那样.但是它不起作用。 MyUserControl 有一个 ItemsControl,它与 List 数据绑定(bind),如下所示。通过 uc.MyCollection = myCollection; 添加的项目未反射(reflect)在 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。

关于c# - 渲染前获取 UserControl 的实际大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8665475/

相关文章:

c# - 如何将 C .DLL 文件导入 C# Windows 窗体应用程序并调用其在 header 中定义的函数?

c# - Silverlight 等同于已签名的 Java Applet?

Silverlight - 获取域信息

c# - 将DataTrigger的 `Value`绑定(bind)到 `VModel`

c# - 如何在 CLR/托管 C++ 中实现变体类型?

c# - 给定 FileStream 确定文件扩展名

c# - 字符串行为的解释

c# - OpenGL 函数 ChangeDisplaySettings (C++) 替代

silverlight - 在设计模式下将数据上下文绑定(bind)到另一个 View 模型

c# - 编辑 ListBox 容器样式