我正在通过Web服务器在WPF应用程序中填充ListView。 加载数据后,ListView会超出其父容器的边界并缩放到无穷大,并且不再可能滚动。
我已经尝试将MaxHeigth设置为父母的ActualHeight,但不会返回预期的结果。
<TabItem x:Name="unassingedErrors" Header="{x:Static p:Resources.MainContentPageUnassingedErrors}">
<StackPanel>
<Separator Grid.Row="1"/>
<!--<Frame Grid.Row="2" Name="MainFrameError" Source="UnassignedErrorPage.xaml" />-->
<Grid>
<Grid.DataContext>
<vme:UnassignedErrorPageVM x:Name="ViewModel"/>
</Grid.DataContext>
<ScrollViewer>
<ListView ItemsSource="{Binding errorList}">
<ListView.Template>
<ControlTemplate>
<ItemsPresenter></ItemsPresenter>
</ControlTemplate>
</ListView.Template>
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="{x:Static p:Resources.ErrorPageID}"/>
<Label Grid.Row="0" Grid.Column="1" Content="{x:Static p:Resources.ErrorPageText}"/>
<Label Grid.Row="0" Grid.Column="2" Content="{x:Static p:Resources.ErrorPageThrowDate}"/>
<Label Grid.Row="0" Grid.Column="3" Content="{x:Static p:Resources.ErrorPageHandledDate}"/>
<Label Grid.Row="0" Grid.Column="4" Content="{x:Static p:Resources.ErrorPageIncident}"/>
<Label Grid.Row="0" Grid.Column="5" Content="{x:Static p:Resources.ErrorPageType}"/>
<Label Grid.Row="0" Grid.Column="6" Content="{x:Static p:Resources.ErrorPageSensor}"/>
<Label Grid.Row="1" Grid.Column="0" Content="{Binding ID}"/>
<Label Grid.Row="1" Grid.Column="1" Content="{Binding Text}"/>
<Label Grid.Row="1" Grid.Column="2" Content="{Binding ThrowDate}"/>
<Label Grid.Row="1" Grid.Column="3" Content="{Binding HandledDate}"/>
<Label Grid.Row="1" Grid.Column="4" Content="{Binding Incident.IncidentID}"/>
<Label Grid.Row="1" Grid.Column="5" Content="{Binding Type.Name}"/>
<Label Grid.Row="1" Grid.Column="6" Content="{Binding Sensor.Name}"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ScrollViewer>
</Grid>
</StackPanel>
</TabItem>
答案 0 :(得分:2)
我具有相同的标记,因此应为ScrollViewer控件设置MaxHeight属性。这是我的标记:
<Grid>
<ScrollViewer x:Name="ScrollViewer" ScrollChanged="ScrollViewer_ScrollChanged" MaxHeight="100">
<ListView ItemsSource="{Binding}">
<ListView.View>
<GridView>
<GridView.ColumnHeaderContainerStyle>
<Style TargetType="GridViewColumnHeader">
<Setter Property="Visibility" Value="Hidden" />
<Setter Property="Height" Value="0" />
</Style>
</GridView.ColumnHeaderContainerStyle>
<GridViewColumn Header="Log" DisplayMemberBinding="{Binding Text}">
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</ScrollViewer>
</Grid>
答案 1 :(得分:2)
实际的问题是,滚动查看器不知道它应该渲染多高,因此它不会显示滚动条。
我的方法是通过绑定将滚动查看器的高度设置为您非常了解的高度。所以我已经将您的代码放入测试窗口并为其命名了
<Window x:Class="MyTest.MainWindow" x:Name="myWindow" >
...
<ScrollViewer Height="{Binding ElementName=myWindow, Path=Content.ActualHeight}" >
...
您还可以使用其他选择的FrameworkElement来获取高度。