Home > Software design >  I have lot of lag/slow down when i use a scrollViewer. Can i fix that?
I have lot of lag/slow down when i use a scrollViewer. Can i fix that?

Time:10-06

I need to create a scrollviewer with X amount of items, i made the code that create a border with an image and sets into a grid, thats the code:

    public partial class MainWindow : Window
    {
        panel panels = new panel();
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            int col = -1;
            int actual_row =0;

            for (int i = 0; i != 20;i  )
            {
                col  ;

                Grid g = panels.create_panel();
                scroll.Children.Add(g);

                g.SetValue(Grid.RowProperty, actual_row);
                g.SetValue(Grid.ColumnProperty, col);

                if (col == 3)
                {
                    actual_row  ;
                    col = -1;

                    //aad row
                    RowDefinition newrow = new RowDefinition();
                    scroll.RowDefinitions.Add(newrow);
                }


            }
        }
    }

and her is the XAML:

<Window x:Class="pretyListbox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:pretyListbox"
        mc:Ignorable="d"
        Background="#282828"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Style x:Key="item" TargetType="Grid">

        </Style>
    </Window.Resources>
    <Grid>
        <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"
                      VirtualizingStackPanel.IsVirtualizing="True" 
                      VirtualizingStackPanel.VirtualizationMode="Standard" 
                      >
            <Grid x:Name="scroll">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>



            </Grid>

        </ScrollViewer>

        <Button  Height="100" Width="100" Click="Button_Click" Content="gen" Margin="0,0,3,1" HorizontalAlignment="Right" VerticalAlignment="Bottom"/>
    </Grid>
</Window>

I put an image of the app working: App working

And there is the lag:Lagged app

the lag only appears the first time it get resized or scrolled, but isnt about time bc i wait 1 min and it get lagg equally. If someone can tell me how to repair that or charge the grid after with an async methot i will be very greatefull. Thnx.

CodePudding user response:

Use ItemControl and Bind all items to ItemSource, It will create Vertical scroll bar if required

Please look at below example

<ItemsControl x:Name="CountryCodeFlagControl" 
                  ItemsSource="{Binding SourceList}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.Template>
            <ControlTemplate>
                <Border>
                    <ScrollViewer>
                        <ItemsPresenter />
                    </ScrollViewer>
                </Border>
            </ControlTemplate>
        </ItemsControl.Template>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Button Content="{Binding Value}">
                </Button>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

CodePudding user response:

i resolve my answer, i just put an asynchronous methot and now it works perfectly every time. The code :


        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            int col = -1;
            int actual_row =0;

            for (int i = 0; i != 100; i  )
            {
                Task task = Task.Factory.StartNew(() =>
                {
                    col  ;

                    Dispatcher.Invoke(() =>
                    {
                        Grid g = panels.create_panel();
                        scroll.Children.Add(g);

                        g.SetValue(Grid.RowProperty, actual_row);
                        g.SetValue(Grid.ColumnProperty, col);

                        if (col == 3)
                        {
                            actual_row  ;
                            col = -1;

                            //aad row
                            RowDefinition newrow = new RowDefinition();
                            scroll.RowDefinitions.Add(newrow);
                        }
                    });
                });
                await Task.Delay(100);
                await task;


            }
        }
  • Related