我在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>
答案 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}}