Search code examples
silverlight-4.0silverlight-toolkitscrollviewerwrappanel

WrapPanel within ItemsControl added ScrollViewer causes loosing wrapping behavior


I'm working with a WrapPanel within an ItemsPanel within an ItemsControl. This works perfect as long as the items added fit the size. To meet the situations where the content exceeds the size, I added a ScrollViewer. But now the items are listed only vertically even if all they would fit. Any idea if it is possible and what I have to do.

Here is my xaml:

<ItemsControl Name="LicSwUserItemsCtl" Grid.Column="1" Grid.Row="2">
<ItemsControl.Template>
    <ControlTemplate>
        <ScrollViewer Name="Scroller" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
            <ItemsPresenter />
        </ScrollViewer>
    </ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
        <toolkit:WrapPanel Orientation="Vertical"  HorizontalAlignment="Stretch" Margin="0" Name="wrpPanel" VerticalAlignment="Stretch" />
    </ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
    <DataTemplate>
        <Border x:Name="BrdrStkPnlSwLicUser" CornerRadius="2" BorderThickness="1" Width="120" Height="20" Margin="0,0,3,0" BorderBrush="Transparent">
            <StackPanel Name="StkPnlSwLicUser"  Orientation="Horizontal" Height="18" Width="120" Margin="0" MouseEnter="StackPanel_MouseEnter" MouseLeave="StackPanel_MouseLeave" MouseLeftButtonUp="StackPanel_MouseLeftButtonUp" >
                <Image Source="{Binding Converter={StaticResource IconImage}}" Margin="2,0,3,0" Height="16" Width="16" />
                <TextBlock Name="txtBlk" Text="{Binding NodeText}" />
            </StackPanel>
        </Border>
    </DataTemplate>
</ItemsControl.ItemTemplate>


Solution

  • I found the solution actually before I posted my problem. But I think this might be useful for somebody else.

    I added a SizeChanged handler to the ScrollViewer.

    SizeChanged="Scroller_SizeChanged"
    

    At the handler I added the following code.

    private void Scroller_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        WrapPanel wrpPanel = (WrapPanel)LicSwUserItemsCtl.GetItemsHost();
        wrpPanel.MaxHeight = ((ScrollViewer)sender).ActualHeight;
        wrpPanel.MaxWidth = ((ScrollViewer)sender).ActualWidth;
    }
    

    I would be interested if this can be achieved by binding the MaxWidth, MaxHeight property in xaml.