1
votes

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.

2
Can you include the usercontrol xaml which has the expander?Mat J
@mathew-jibin the code is:<code> <StackPanel VerticalAlignment="Top" Height="Auto"> <Expander> <ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.CanContentScroll="False" ItemsSource="{Binding MyCollection}"> <ListBox.ItemTemplate> <DataTemplate> <local:MyUserControl /> </DataTemplate> </ListBox.ItemTemplate> </ListBox></code>Aave

2 Answers

1
votes

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

0
votes

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.