简体   繁体   中英

WPF: ComboBox Max Items

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>

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

the viewmodel class

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

                          }
                      });

the Asynchrounous method :

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.

So I need to know :

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

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM