2014-11-08 158 views
1

我有一個TabItem裏面有很多元素,我想讓ListView可以垂直滾動但是讓按鈕保持在頂部(不滾動)。我嘗試在ScrollViewer中包裝ListView,但它不會滾動。我可以將ScrollViewer放在ItemsControl之外,並更新RowDefinition Height="*"以及包括按鈕在內的所有內容滾動,但這不是我所需要的。ListView不會滾動垂直

XAML:

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="Auto" /> 
    </Grid.RowDefinitions>  

    <Menu Grid.Row="0" KeyboardNavigation.TabNavigation="Cycle"> 
     <MenuItem Header="File"> 
      <MenuItem Header="Exit" /> 
     </MenuItem> 
     <MenuItem Header="Options" /> 
     <MenuItem Header="Help" /> 
    </Menu> 

    <TextBlock Grid.Row="1" Text="Test" Height="23" Width="Auto" HorizontalAlignment="Left" TextWrapping="Wrap"/> 

    <TabControl Grid.Row="2" > 
     <TabItem Header="TabItem1"> 
      <ItemsControl> 
       <StackPanel Orientation="Horizontal"> 
        <Button Command="{Binding Button1}" Width="150" Margin="5" Height="22" HorizontalAlignment="Left">Button1</Button> 
        <Button Command="{Binding Button2}" CommandParameter="{Binding ElementName=listView}" Width="150" Margin="5" Height="22" HorizontalAlignment="Left">Button2</Button> 
       </StackPanel> 
        <ScrollViewer> 
         <ListView Name="listView" Grid.Row="1" BorderThickness="0" ItemsSource="{Binding myItems}"> 
          <ListView.View> 
           <GridView> 
            <GridViewColumn Header="ID" DisplayMemberBinding="{Binding ID}" Width="Auto" /> 
            <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="Auto" /> 
           </GridView> 
          </ListView.View> 
         </ListView> 
        </ScrollViewer> 
       </ItemsControl> 
     </TabItem> 

     ... Other tabs 
    </TabControl> 
</Grid> 

回答

3

你不需要ItemsControl,這是默認情況下將商品放進垂直StackPanel這給它的兒童身高增長的無限量。換句話說,ListView有無限的高度來適應所有項目,因此不會顯示滾動條。改爲使用Grid,然後您不需要ScrollViewer。它的默認模板的一部分ListView反正

<TabItem Header="TabItem1"> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition/> 
     </Grid.RowDefinitions> 
     <StackPanel Orientation="Horizontal" Grid.Row="0"> 
      <Button Command="{Binding Button1}" Width="150" Margin="5" Height="22" HorizontalAlignment="Left">Button1</Button> 
      <Button Command="{Binding Button2}" CommandParameter="{Binding ElementName=listView}" Width="150" Margin="5" Height="22" HorizontalAlignment="Left">Button2</Button> 
     </StackPanel> 
     <ListView Name="listView" Grid.Row="1" BorderThickness="0" ItemsSource="{Binding myItems}"> 
      <ListView.View> 
       <GridView> 
        <GridViewColumn Header="ID" DisplayMemberBinding="{Binding ID}" Width="Auto" /> 
        <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="Auto" /> 
       </GridView> 
      </ListView.View> 
     </ListView> 
    </Grid> 
</TabItem> 

也在主Grid沒有設定最後一行(一個包含TabControl)高度自動,因爲這會給TabControl的空間,以適應整個ListView無需滾動

<Grid.RowDefinitions> 
    <RowDefinition Height="Auto" /> 
    <RowDefinition Height="Auto" /> 
    <RowDefinition /> 
</Grid.RowDefinitions> 
+0

完美。感謝您的解釋! – yellavon 2014-11-08 18:12:30

+0

沒問題@yellavon。很高興幫助 – dkozl 2014-11-08 18:17:01