WPF ComboBox性能问题通过绑定大集合 [英] WPF ComboBox performance problems by binding a large collections

查看:1132
本文介绍了WPF ComboBox性能问题通过绑定大集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图绑定一个大集合到一个ComboBox和我面临的性能问题时打开ComboBox的弹出窗口。我搜索互联网,发现使用VirtualizingStackPanel作为项目面板模板可能有帮助,但它只有部分帮助。如果我将一个大的集合绑定到一个ComboBox,我可以很快打开弹出窗口,没关系,但如果之后我绑定另一个集合到ComboBox并尝试再次打开弹出窗口,它变得非常慢。如果你为一个空的ComboBox打开弹出窗口,然后绑定大的集合,并尝试再次打开弹出窗口,这会发生同样的情况。弹出窗口打开之前需要几秒钟。



这里是XAML :

 < ComboBox Name =cbBlah> 
< ComboBox.ItemsPanel>
< ItemsPanelTemplate>
< VirtualizingStackPanel />
< / ItemsPanelTemplate>
< /ComboBox.ItemsPanel>
< / ComboBox>

和用于绑定的示例代码来重现问题:

  var list = new List< string>(); 
for(var i = 0; i< new Random()。Next(9000,10000); i ++)
list.Add(i.ToString());
cbBlah.ItemsSource = list;

我试图使虚拟化堆栈面板看起来像这样:

 < VirtualizingStackPanel VirtualizingStackPanel.IsVirtualizing =TrueVirtualizingStackPanel.VirtualizationMode =Recycling/> 

但它没有帮助,似乎VirtualizationMode被忽略,所以弹出窗口只打开非常快,

:我想每次都不绑定新集合,但绑定一个ObservableCollection一次然后只是改变它的内容。同样的,一旦收集内容改变,打开一个弹出窗口还需要几秒钟:(

解决方案

http:// vbcity。 com / blogs / xtab / archive / 2009/12/15 / wpf-using-a-virtualizingstackpanel-to-improve-combobox-performance.aspx



我用这个代码测试过:

 < ComboBox Name =cbBlahItemsSource ={Binding}& 
< ComboBox.ItemsPanel>
< ItemsPanelTemplate>
< VirtualizingStackPanel />
< / ItemsPanelTemplate>
< /ComboBox.ItemsPanel>
< / ComboBox>

它第一次和下一次工作正常。代码这些行:

 < VirtualizingStackPanel VirtualizingStackPanel.IsVirtualizing =TrueVirtualizingStackPanel.VirtualizationMode =Recycling/& 

我希望这可以帮助你。


I'm trying to bind a large collection to a ComboBox and I faced performance problems when opening ComboBox's popup. I searched internet and found that using VirtualizingStackPanel as a items panel template might help, but it helped only partially. If I bind a large collection to a ComboBox, I could open popup very quickly, that's ok, but if after that I bind another collection to a ComboBox and try to open popup again, it becomes very slow. Same is happening if you open popup for an empty ComboBox, then bind large collection and try to open popup again - it takes some seconds before popup opens.

Here is the XAML:

<ComboBox Name="cbBlah">
    <ComboBox.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel />
        </ItemsPanelTemplate>
    </ComboBox.ItemsPanel>
</ComboBox>

and the sample code for binding to reproduce the problem:

var list = new List<string>();
for (var i = 0; i < new Random().Next(9000, 10000); i++)
    list.Add(i.ToString());
cbBlah.ItemsSource = list;

I tried to make virtualizing stack panel to look like this:

<VirtualizingStackPanel VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling" />

but it doesn't help, seems VirtualizationMode is ignored so popup opens very fast only first time and then, each time after binding changes, it's very slow.

UPDATE: I thought about not binding new collection every time, but bind an ObservableCollection once and then just changing its content. Same thing, as soon as content of collection changes, opening a popup still takes several seconds :(

解决方案

According to this blog: http://vbcity.com/blogs/xtab/archive/2009/12/15/wpf-using-a-virtualizingstackpanel-to-improve-combobox-performance.aspx

I've tested it with this code:

<ComboBox Name="cbBlah" ItemsSource="{Binding}">
    <ComboBox.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel />
        </ItemsPanelTemplate>
    </ComboBox.ItemsPanel>
</ComboBox>

It works fine for first time and next times. It's not necessary to code these lines:

<VirtualizingStackPanel VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling" />

I hope this helps you.

这篇关于WPF ComboBox性能问题通过绑定大集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆