سؤال

I have a grid. This grid holds a scrollviewer which holds a listbox whose items are expanders. When expanding the expanders until they exceed the vertically available height of the UserControl, the scrollbar of the scrollviewer appers correctly. When I collapse the expanders again, the scorll bar of the scrollviewer stays the same! It doesn't shrink or disapper again as it would be expected.

I already tried some different grid row settings ("Auto", "*") and different Vertical Alignment settings (Top, Stretch,...)

How can I manage to make the scrollviewer shrink again after collapsing the expanders?

<UserControl x:Class="Expanderin_Scroller.MainPage"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
             mc:Ignorable="d"
             d:DesignWidth="440"
             d:DesignHeight="300">

  <Grid x:Name="LayoutRoot">
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
      <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    <ScrollViewer x:Name="ScrToolbox"
                  Grid.Row="1"
                  VerticalAlignment="Top">
      <ListBox x:Name="Toolbox">
        <telerik:RadExpander Header="Expander 1">
          <StackPanel>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
          </StackPanel>
        </telerik:RadExpander>

        <telerik:RadExpander Header="Expander 2">
          <StackPanel>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
          </StackPanel>
        </telerik:RadExpander>

        <telerik:RadExpander Header="Expander 3">
          <StackPanel>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
          </StackPanel>
        </telerik:RadExpander>

        <telerik:RadExpander Header="Expander 4">
          <StackPanel>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
            <TextBlock>Hello</TextBlock>
          </StackPanel>
        </telerik:RadExpander>
      </ListBox>
    </ScrollViewer>
  </Grid>
</UserControl>
هل كانت مفيدة؟

المحلول

I could solve the issue with the help of the Telerik guys:

This issue is caused by the ListBox behavior and specifically its ItemsPanel. In order to update the size of the ListBox along with the size of its content you can set the ListBox.ItemsPanel to a StackPanel.

<ListBox.ItemsPanel>
     <ItemsPanelTemplate>
          <StackPanel />
     </ItemsPanelTemplate>
</ListBox.ItemsPanel>

Now the Listbox resizes along with its content.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top