我有一个开着VirtualizingStackPanel的TreeView。TreeView有一个"Level 1“TreeViewItem,我将这个TreeViewItem绑定到一个10k条目列表。每个子对象也是另一个TreeViewItem。
虚拟化工作得很好,性能也很好,但有一个大问题。假设我在页面的顶部,按Ctrl-End组合键到达底部,浏览器变为空白。如果我稍微滚动一下鼠标或者调整浏览器的大小,它就会重新出现。
另一个大问题是:当我快速滚动到树的中间或底部时。假设我在5000号商品上停下来。然后我不能展开子树,浏览器什么也不会显示,直到我滚动或稍微调整一下大小。
任何帮助都是非常感谢的。以下是示例xaml代码:
<Page x:Class="WpfBrowserApplication3.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:s="clr-namespace:WpfBrowserApplication3"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="Page1" DataContext="{StaticResource _mainViewModel}">
<Page.Resources>
<s:SingleToCollectionConverter x:Key="_collectionConverter"></s:SingleToCollectionConverter>
<DataTemplate x:Key="_level2Template">
<TreeViewItem Header="{Binding Order}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Order: "></TextBlock>
<TextBox Text="{Binding Order}"></TextBox>
</StackPanel>
</TreeViewItem>
</DataTemplate>
</Page.Resources>
<TreeView VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Standard">
<TreeViewItem Header="Level 1" ItemsSource="{Binding Level2List}" ItemTemplate="{StaticResource _level2Template}"></TreeViewItem>
</TreeView>
</Page>
public class MainViewModel
{
public MainViewModel()
{
Level2List = new List<Level2>();
for (int i = 0; i < 10000; i++)
{
Level2List.Add(new Level2(i));
}
}
public List<Level2> Level2List { get; set; }
}
public class Level2
{
public Level2(int order)
{
Order = order;
}
public int Order { get; set; }
}我使用的是Visual Studio2010和.Net 4。另外,我注意到如果我在_level2Template下设置了TreeViewItem的高度和宽度,这个问题就解决了。但在我的例子中,设置高度不是一个选项,因为在实际的应用程序中高度是不同的。
更新:对我来说,这个问题很明显,因为孩子树的高度可能会不同。也许这就是为什么VirtualizingStackPanel在TreeView中没有默认打开,但在DataGrid & ListBox中默认打开的原因。不用说,datagrid或listbox项的高度通常是不变的。
更新:我下载了telerik RadTreeView的免费试用版,并测试了虚拟化。这个问题在telerik radtreeview中根本不会出现。可能会对telerik one进行更多的测试,然后可能会使用它。
发布于 2011-03-17 20:26:39
发现了这个:TreeView Virtualization同样的问题,并且没有针对TreeView的解决方案。唯一的解决方法是使用ListBox而不是http://www.beacosta.com/blog/?p=45中建议的TreeView
我很确定这是TreeView的错误。我试过Telerik RadTreeView,它在开启虚拟化的时候也有它自己的bug。我将改为使用ListBox。
https://stackoverflow.com/questions/5295261
复制相似问题