我的CustomItemsControl如下所示:
根据我的CustomItemsControl包含的项目数量,它应该计算“容器”项的宽度和高度 .
我以为我可以通过调用项目的测量/排列方法来实现这一点 . 但是我的代码似乎对项目的大小没有任何影响((实际)宽度或(实际)高度是NaN或0)
public class CustomItemsControl : ItemsControl
{
protected override Size MeasureOverride(Windows.Foundation.Size availableSize)
{
Windows.Foundation.Size size = base.ArrangeOverride(availableSize);
if (ItemsSource != null)
{
double CellWidth = size.Width / Items.Count;
foreach (var Item in Items)
{
DependencyObject Container = ContainerFromItem(Item);
if(Container!=null)
{
FrameworkElement Element = Container as FrameworkElement;
//Element.Width = CellWidth;
//Element.Height = CellWidth;
Element.Measure(new Size(CellWidth, CellWidth));
Element.Arrange(new Rect(0, 0, CellWidth, CellWidth));
}
}
}
return size;
}
}
除非您设置边框的宽度和高度(例如10),否则不会显示这些项目 . 我尝试完成我的CustomItemsControl计算项目的宽度和高度 . 我做错了什么?我怎样才能完成我的计划?