I tried the suggestion here regarding Stretching, but it didn't work for me.
I have a TabItem that hosts a UserControl that is essentially just a ListBox with an ItemsPanel. The ItemsPanel forces the ListBox to display its contents horizontally. The contents of the ListBox are databound to an ObservableCollection<StackPanelContents>
, which is another UserControl. StackPanelContents
itself basically just contains a List
of objects with some visual representation (they contain a UIElement for visualization).
In general the code works properly in that it renders all of the data that I have contained in the ObservableCollection
, but the problem is that the items don't resize as the window is enlarged or shrunk.
Here's an overview of what the class hierarchy looks like:
And here are the results:
The XAML for the main window just has the TabControl and TabItem:
<Window x:Class="ResizeStackPanelInListBox.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<TabControl>
<TabItem Header="Test" x:Name="MyTabItem">
<!-- contents are set in code behind -->
</TabItem>
</TabControl>
</Window>
The ListBoxContainer XAML that displays the StackPanelContents looks like this:
<UserControl x:Class="ResizeStackPanelInListBox.ListBoxContainer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="Auto" Width="Auto">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock>ListBox with StackPanels as its ListBoxItems:</TextBlock>
<ListBox Grid.Row="1" ItemsSource="{Binding StackPanels}" HorizontalContentAlignment="Stretch">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" Width="Auto" Height="Auto" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</Grid>
</UserControl>
And the StackPanelContents UserControl looks like this:
<UserControl x:Class="ResizeStackPanelInListBox.StackPanelContents"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="Auto" Width="Auto">
<StackPanel>
<TextBlock Text="StackPanel"></TextBlock>
<StackPanel x:Name="MyStackPanel">
</StackPanel>
</StackPanel>
</UserControl>
Can anyone explain why this isn't resizing automatically, and how I can go about resolving this? Am I going to have to write my own custom panel to deal with this, and use MeasureOverride
and Arrange
?