If I use a custom panel to layout my ListBoxItems, the ListBox won't respect their combined Height (it does respect their combined Width though) - even when my ArrangeOverride returns a size that surrounds all the items.
Setting the ListBox's Height explicitly makes everything work, but I want it to work that out for itself!
Has anyone seen this before?
Thanks
Update: In the example below, the ListBox uses a custom panel that stacks the Articles vertically according to the Row property and returns a size big enough to surround all of them. But unless I set the Height for the ListBox it collapses!
<UserControl x:Class="SilverlightApplication1.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SilverlightApplication1">
<UserControl.Resources>
<DataTemplate x:Key="ArticleTemplate">
<TextBlock Text="{Binding Title}" />
</DataTemplate>
</UserControl.Resources>
<ListBox Height="200"
Background="AntiqueWhite"
ItemTemplate="{StaticResource ArticleTemplate}"
ItemsSource="{Binding}" VerticalAlignment="Top"
Margin="0,0,0,0">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<local:MyPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</UserControl>
Here is the panel:
public class MyPanel : Panel
{
protected override Size ArrangeOverride(Size arrangeSize)
{
double width = 0, height = 0;
foreach (UIElement child in this.Children)
{
var article = (Article)((ContentControl)child).DataContext;
var y = child.DesiredSize.Height * article.Row;
var location = new Point(0, y);
var rect = new Rect(location, child.DesiredSize);
child.Arrange(rect);
width = Math.Max(width, child.DesiredSize.Width);
height = Math.Max(height, y + child.DesiredSize.Height);
}
return new Size(width, height);
}
protected override Size MeasureOverride(Size availableSize)
{
foreach (UIElement child in this.Children)
{
if (child != null)
{
child.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
}
}
return new Size();
}
}
And the domain class:
public class Article
{
private readonly int row;
private readonly string title;
public Article(string title, int row)
{
this.title = title;
this.row = row;
}
public int Row { get { return this.row; } }
public string Title { get { return this.title; } }
}