My CustomItemsControl looks like the following:
<local:CustomItemsControl x:Name="CustomItemsControl" >
<local:CustomItemsControl.ItemTemplate>
<DataTemplate>
<Border x:Name="rectangle" Background="Orange" CornerRadius="5" />
</DataTemplate>
</local:CustomItemsControl.ItemTemplate>
</local:CustomItemsControl>
Depending on the amount of the items which my CustomItemsControl contain, it should calculate the Width and the Height of "Container" Items.
I thought I could achieve that by calling the methods Measure/Arrange of the Items. But my Code doesn't seem to have any effects on the Item's size ((Actual)Width or (Actual)Height is NaN or 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;
}
}
The items will not be displayed unless you set the Width and Height of the Border (e.g. 10). I try to accomplish that my CustomItemsControl calculates the Width and Height of the Items. What did I do wrong? How can I accomplish my plan?