我正在尝试在UserControl
类中使用Measure
方法,在MyUserControl
被渲染之前获取其的实际大小,这是为了解决我之前提出的问题。然而这种方法并不起作用。 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