Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I develop an application C# WPF.

In my XAML i have an ItemsControl with my List in ItemsSource, in binding. For example :


XML
<ItemsControl x:Name="lvDataBinding" Foreground="AliceBlue" ItemsSource="{Binding LstAvis}" Margin="0" ScrollViewer.CanContentScroll="True" VirtualizingPanel.IsVirtualizing="True" VerticalAlignment="Top">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Border BorderBrush="White" BorderThickness="1" CornerRadius="3" Margin="{Binding strMargin}" Opacity="{Binding dblOpacityCurrent}" HorizontalAlignment="Left" VerticalAlignment="Top">
                            <Grid Width="{Binding dblWidth}" Height="{Binding strHeightGrid}" Tag="{Binding strNumAvis}" Margin="0.1" Background="{Binding scbColorCurrentSelected}"  >
                           </Grid>
                        </Border>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <UniformGrid Columns="1"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>


Then in my behind code i have a function in a thread to fill a list :


C#
public void NewConstruct_View()
        {
            Application.Current.Dispatcher.Invoke(new Action(() => _lstAvistemp.Clear()));

            Dispatcher.Invoke(new Action(() => Mouse.OverrideCursor = Cursors.Wait));

            //Insere la liste dans l'objet qui permettra son affichage
            for (int i = 0; i <= dt_DATA.Rows.Count - 1; i++)
            {
                Avis = new CL_Avis();
              
                Avis.strNumAvis = dt_DATA.Rows[i]["QMNUM"].ToString();
        
                Application.Current.Dispatcher.Invoke(new Action(() => _lstAvistemp.Add(Avis)));
            }

            

            Dispatcher.Invoke(new Action(() => LstAvis.ReplaceRange(_lstAvistemp)));
            Dispatcher.Invoke(new Action(() => { }), DispatcherPriority.ContextIdle, null);
        }


But my application need 5 seconds to execute
C#
Dispatcher.Invoke(new Action(() => { }), DispatcherPriority.ContextIdle, null);
and display my grid.

I have only 500 rows, i think it's very slow.

I simplified the code for reading, but i have only about 20 fields (it's not a lot...)

I worked in a thread, my list in a list of type RangeOBservableCollection.

Thank you for your help.

What I have tried:

I saw i have had some exceptions :

Quote:
System.Windows.Data Error: 23 : Cannot convert '<null>' from type '<null>' to type 'System.Windows.Visibility' for 'en-US' culture with default conversions; consider using Converter property of Binding. NotSupportedException:'System.NotSupportedException: Conversion de EnumConverter impossible à partir de (null).
à System.ComponentModel.TypeConverter.GetConvertFromException(Object value)
à System.ComponentModel.TypeConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
à System.ComponentModel.EnumConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
à MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward)'


I corrected this exception. I had a variable for visibility in my binding null, i filled this variable. It's ok.
Posted
Updated 21-Jun-18 9:20am
v2

If you're not already doing it, instead of using a normal list, use an ObservableCollection.

All of the control population will happen without you having to write any code.

Another technique is to wait until the form is shown before you load the data so you can show a wait cursor during the load.
 
Share this answer
 
Lots of things wrong with your code; most of all, using a "non-virtualizing" UniformGrid. You need to stick with the basics.

ItemsControl.ItemsPanel Property (System.Windows.Controls)[^]
 
Share this answer
 
Comments
Steve15f 25-Jun-18 1:06am    
Hi
What do i need to change ? Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900