在渲染之前获取UserControl的实际大小

dev*_*ull 6 c# silverlight

我想获得的ActualSize MyUserControl以前被使用渲染Measure的梅索德UserControl作为建议我的课前一个问题.但它不起作用.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
Run Code Online (Sandbox Code Playgroud)

Sto*_*one 6

你必须覆盖你的uc中的MeasureOverride和ArrangeOverride来计算你自己的大小.在MeasureOverride里面用你自己大小的"测量""询问"你的列表 - 所以你可以计算你自己的uc的大小(在MeasureOverride中返回这个大小) - 然后你在调用uc.Measure后得到你的DesiredSize.

  • 我很确定你只需要在自定义面板实现上实现这些方法.UC只是其他控件的组合,因此没有必要实现这些控件. (2认同)