ListView不会垂直滚动

时间:2014-11-08 17:18:34

标签: c# wpf listview scrollviewer

我在TabItem中有许多元素,我想让ListView垂直滚动,但按钮保持在顶部(不滚动)。我尝试将ListView包裹在ScrollViewer中,但不会滚动。我可以将ScrollViewer放在ItemsControl之外并更新RowDefinition Height="*"并滚动所有内容,包括按钮,但这不是我需要的。

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>

1 个答案:

答案 0 :(得分: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> 无滚动

{{1}}