2011-03-22 55 views
2

我在WPF窗口中使用ItemsControl。 itemscontrol綁定到一個集合,集合是一組視圖模型(用戶控件)。我的問題 - 由於集合中有很多視圖模型,視圖超出了當前窗口。我嘗試了很多事情來處理它與滾動條,但沒有用。有什麼建議麼?真正的問題是如何在窗口中包含itemscontrol(滾動)?ItemsControl-包裝窗口內的內容

的XAML下面

<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> 

回答

6

包裝你在ScrollViewer

更新ItemsControl:在你的榜樣,還設置行高爲*如果不能正確縮放。

+1

嗯,我做到了,但滾動查看器本身超出了窗口,我無法滾動 – Jimmy 2011-03-22 16:26:32

2

除了@ Bas的回答,請將您的MaxHeightMinHeight屬性設置爲您的窗口尺寸。您可以使用示例中包含的硬編碼數字,也可以創建與Window.ActualHeight/ActualWidth的綁定。

+0

這是需要在WPF中?也許將第一行改爲*也應該這樣做。設置MaxHeight和MinHeight根本不算優雅。 – Bas 2011-03-22 18:11:07

+0

@你說的對,在代碼發佈中將第一行高設置爲*會更好。我沒有注意到:) – Rachel 2011-03-22 18:17:33