WPF:ComboBox最大项目 [英] WPF: ComboBox Max Items

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

问题描述

我有一个WPF应用程序,其中有一个组合框

I have a WPF application in which I have a combobox

   <ComboBox  Margin="2,0,5,0" Width="178" ItemsSource="{Binding Animateur}" DisplayMemberPath="fsign_nom"   SelectedIndex="0"  >
                                <ComboBox.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <VirtualizingStackPanel />
                                    </ItemsPanelTemplate>
                                </ComboBox.ItemsPanel>
 </ComboBox>

ItemsSource 包含20100个项目,是我尝试打开组合框选择一个元素时,应用程序被阻止。

the ItemsSource contains 20100 items , the problem is when I try to open the combobox to select an element, the application were blocked .

viewmodel类

_service.GetAnimateur((item, error) =>
                      {
                          if (error != null)
                          {
                              // TODO : traitement d'erreur
                          }
                          else
                          {
                              _Animateur.Clear();
                              item.ForEach(Elem =>
                              {
                                  _Animateur.Add(Elem);
                              });

                          }
                      });

Asynchrounous方法

public async void GetAnimateur(Action<List<fiche>, Exception> callback)
        {
            try
            {
                Task<List<fiche>> data = (Task<List<fiche>>)Task.Run(
                    () =>
                    {
                        DataEntities _db = new DataEntities();
                        _db.Configuration.LazyLoadingEnabled = false;
                        var dpcs = _db.fiche;
                        return new List<fiche>(dpcs);
                    });
                var result = await data;
                callback(result, null);
            }
            catch (Exception ex)
            {
                callback(null, ex);
            } 
        }



我看过这个文章,所以我添加了虚拟化部分,但我有相同的结果。

I have seen this article so I added the virtualization part, but I got the same result.

所以我需要知道:


  1. 如何解决这个问题?

  2. ItemsSource 中的项目数量上限是多少?

  1. How can I fix this problem?
  2. What is the max number of items in the ItemsSource ?


推荐答案

尝试测试没有异步部分

这对我100万行非常有用

Try testing without the async part
This works fine for me with 1 million rows

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <ComboBox Grid.Row="0" ItemsSource="{Binding Path=Lots}">
        <ComboBox.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel />
            </ItemsPanelTemplate>
        </ComboBox.ItemsPanel>
    </ComboBox> 
    <ListBox  Grid.Row="1" ItemsSource="{Binding Path=Lots}" 
                VirtualizingStackPanel.IsVirtualizing="True"/>
</Grid>

private List<string> lots;
public  List<string> Lots
{
    get
    {
        if (lots == null)
        {
            lots = new List<string>();
            for (int i = 0; i < 1000000; i++) lots.Add("lot " + i.ToString());
        }
        return lots;
    }
}

这篇关于WPF:ComboBox最大项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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