我正在尝试获取 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/