我想使用ItemsControl显示一个重要的项目列表。
我使用ItemsControl的原因是,在我正在处理的应用程序中,DataTemplate要复杂得多:提供的示例代码只反映了我遇到的大小调整问题。
我想:
背后的代码是:
public partial class Page1: Page
{
public List<string> Names { get; set; }
public Page1()
{
InitializeComponent();
Names = new List<string>();
for(int i = 0; i < 10000; i++)
Names.Add("Name : " + i);
My.DataContext = this;
}
}当我强制ScrollViewer高度达到400 to时,ItemsControl虚拟化就像我预期的那样工作:不管它包含多少项,ItemsControl显示列表的速度都非常快。
但是,如果删除Height=“400 to”,则列表将扩展其高度以显示整个列表,而不管其父容器的高度如何。更糟的是:它出现在它的容器后面。
在ItemsControl周围放置一个滚动查看器会提供预期的可视化结果,但是虚拟化会消失,列表需要太多的时间来显示。
如何实现ItemsControl的自动高度扩展和虚拟化?
发布于 2009-10-16 14:34:35
问题在于ItemsControl.Template:您可以在那里使用StackPanel,这样她的孩子就能得到他们想要的高度。把它替换成
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel>
<TextBlock Text="this is a title" FontSize="15" />
<TextBlock Text="This is a description" />
</StackPanel>
<ScrollViewer CanContentScroll="True" Grid.Row="1">
<VirtualizingStackPanel />
</ScrollViewer>
</Grid>而且它应该能正常工作。
希望能帮上忙。
https://stackoverflow.com/questions/1576623
复制相似问题