2

I am using an ItemsControl inside a window in WPF. The itemscontrol is bound with a collection and the collection is a group of view models(user controls). My problem - the view is going beyond the current window as a result of many view models in the collection. I tried many things to handle it with a scroll bar but no use. Any suggestions? The question really is how to contain the itemscontrol within a window(with scrolling)?

The XAML below

<Window x:Class="WpfApplicationTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:app="clr-namespace:WpfApplicationTest"
    Title="MainWindow" Height="350" Width="525">   
  <Grid Height="Auto">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="30"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <ItemsControl Grid.Row="0" Grid.Column="0" ItemsSource="{Binding UserControlCollection}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>           
    <StackPanel Grid.Row="1" Grid.Column="0" Orientation="Horizontal" VerticalAlignment="Bottom">
        <Button Content="OK" Width="100" Margin="3" />
        <Button Content="Cancel" Width="100" Margin="3" />
    </StackPanel>
</Grid>

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Jimmy
  • 3,224
  • 5
  • 29
  • 47

2 Answers2

6

Wrap your ItemsControl in a ScrollViewer

Update: in your example, also set row height to * if it won't scale correctly.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Bas
  • 26,772
  • 8
  • 53
  • 86
  • 1
    Well-I did that, but the the scroll viewer itself is going beyond the window and I cannot scroll – Jimmy Mar 22 '11 at 16:26
2

In addition to @Bas's answer, set your MaxHeight and MinHeight properties to your window dimensions. You can either use the hard-coded numbers you have in the example, or create a binding to Window.ActualHeight/ActualWidth.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Rachel
  • 130,264
  • 66
  • 304
  • 490
  • Is this needed in WPF? Perhaps changing the first row to * should also do it. Setting MaxHeight and MinHeight is not elegant at all. – Bas Mar 22 '11 at 18:11
  • @Bas you're right, setting the first row height to * in the code posted works better. I didn't notice that :) – Rachel Mar 22 '11 at 18:17