我正在尝试使用Measure
方法得到MyUserControl
的实际大小之前类的建议为我的previous question。但它不起作用。 MyUserControl
有一个ItemsControl
,它的数据绑定是List
,如下所示。通过uc.MyCollection = myCollection;
添加的项目未反映在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
尝试在调用'Mesaure()'之前在根上添加'uc'并在访问'DesiredSize'之前执行'UpdateLayout()'。那就是我通常所做的。 – kev 2011-12-29 08:31:51
这不起作用。 – devnull 2011-12-29 08:37:19
@neutrino:什么不工作?值是。 0还是不准确? – Tigran 2011-12-29 08:59:32