Aave
Aave

Reputation: 568

WPF: UserControl with Expander leave blank space after Expander collapsed

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.

Upvotes: 1

Views: 1059

Answers (2)

Aave
Aave

Reputation: 568

Finally, i've solved this problem. The solution is set ScrollViewer.CanContentScroll property of ListBox element to True.

Upvotes: 1

Peter
Peter

Reputation: 1687

The solution is to set the parent FrameworkElement with a HorizontalAligment like in the following example.

<StackPanel Background="Green" VerticalAlignment="Top" >
        <Expander>
            <ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <local:SomeUserControl></local:SomeUserControl>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Expander>
</StackPanel>

This works on my solution.

Your FrameworkElements behaviour around your expander is a bit weird. If you still have some issues you should show more information about it.

Upvotes: 0

Related Questions