I have UserControl with Expander. And I use it in main window in ListBox like this:
<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ItemsSource="{Binding MyCollection}">
<ListBox.ItemTemplate>
<DataTemplate>
<local:MyUserControl />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
After form loaded UserControls fill space normally.
After child user control expanded parent container's height grows, as expected. After child user control expanded
If I collapsed UserControl, there are the blank space left in the parent container. Blank space left after child user control collapsed
If main window gets resize event, all blank space are gone.
I'd tried use Element.InvalidateVisual and Element.InvalidateMeasure in code, and also UpdateLayout, and wrap ListBox with DockPanel + DockPanel.Dock="Top" for panel's child. It was not effect.
At other hand, when I add the same UserControl in the code, this artefact disappears: parent control collapsed as I need after child UserControl collapsed. But I need exactly XAML and UserControl as DataTemplate.
Please, help me. I sure there must be some easy solution, but I'm new to the XAML and so I have difficulty with this.