wpfscrollvieweritemscontrolwrappanel

Can't get ScrollViewer to work within ItemsControl>WrapPanel


I have the following code:

<ItemsControl
  ItemsSource="{Binding ItemsList}"
  Visibility="{Binding HmiState, Converter={StaticResource IsHmiViewState}}">
  <ItemsControl.ItemsContainerStyle>
    <Style>
      <Setter Property="FrameworkElement.Margin" Value="5" />
    </Style>
  </ItemsControl.ItemsContainerStyle>
  <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
      <WrapPanel Width=300 Height=200 ItemsHost="True"
           ScrollViewer.CanContentScroll="True"
           ScrollViewer.VerticalScrollVisibility="Auto" />
    </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>
</ItemsControl>

It display everything up to the 200 height correctly (wrapped), but doesn't display anything below that and there's no scroll bar.
ItemsList is an ObservableCollection.

Does anyone have any suggestions?


Solution

  • There should be a ScrollViewer that hosts an ItemsPresenter in the ControlTemplate of the ItemsControl:

    <ItemsControl ItemsSource="{Binding ItemsList}" Width="300" Height="200">
        <ItemsControl.Template>
            <ControlTemplate TargetType="ItemsControl">
                <ScrollViewer>
                    <ItemsPresenter/>
                </ScrollViewer>
            </ControlTemplate>
        </ItemsControl.Template>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemContainerStyle>
            <Style TargetType="ContentPresenter">
                <Setter Property="Margin" Value="5" />
            </Style>
        </ItemsControl.ItemContainerStyle>
    </ItemsControl>