其他分享
首页 > 其他分享> > 填充ListBox DataTemplate的更好的性能

填充ListBox DataTemplate的更好的性能

作者:互联网

我有一个具有DataTemplate的ListBox.此DataTemplate填充在CodeBehind中,但性能确实很差.填充它的方法在构造函数中调用.请告诉我如何改善它

 <ListBox  ScrollViewer.HorizontalScrollBarVisibility="Visible"
                                  ScrollViewer.CanContentScroll="False"
                                  Name="lbCars"
                                  VirtualizingStackPanel.IsVirtualizing="True" 
                                  VirtualizingStackPanel.VirtualizationMode="Recycling" 
                                  IsSynchronizedWithCurrentItem="True">
                        <ListBox.ItemContainerStyle>
                            <Style TargetType="ListBoxItem">
                                <Setter Property="MaxHeight" Value="100"/>
                            </Style>
                        </ListBox.ItemContainerStyle>
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="auto" />
                                        <ColumnDefinition Width="auto" />
                                        <ColumnDefinition Width="*" />
                                    </Grid.ColumnDefinitions>
                                    <Image Height="auto" Width="125" Source="{Binding car_img_src}" Grid.Column="0" />
                                    <VirtualizingStackPanel Margin="5, 0, 0, 0" Grid.Column="1" Orientation="Vertical" VerticalAlignment="Center">
                                        <TextBlock Text="ID:" FontSize="12"  FontWeight="Bold" Margin="0, 0, 0, 2" />
                                        <TextBlock FontWeight="Bold" FontSize="12" Text="Name:"  Margin="0, 0, 0, 2"/>
                                        <TextBlock FontSize="12" Text="Kategorie:"  FontWeight="Bold" Margin="0, 0, 0, 2"/>
                                        <TextBlock FontSize="12" Text="Tuning:"  FontWeight="Bold"/>
                                    </VirtualizingStackPanel>
                                    <VirtualizingStackPanel Grid.Column="2" Orientation="Vertical" VerticalAlignment="Center" Margin="5, 0, 0, 0">
                                        <TextBlock FontSize="12" Text="{Binding car_id}" Margin="0, 0, 0, 2"/>
                                        <TextBlock FontSize="12" Text="{Binding car_name}" Margin="0, 0, 0, 2"/>
                                        <TextBlock FontSize="12" Text="{Binding car_group}" Margin="0, 0, 0, 2"/>
                                        <TextBlock FontSize="12" Text="{Binding car_tuning}"/>
                                    </VirtualizingStackPanel>
                                </Grid>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

现在如何填充它:

        public MainWindow() {
        InitializeComponent();
        InitializeRsc();
    }

InitializeRsc:

carList.Add(new carEntry { car_id = 400, car_group = "Off Road", car_img_src = "Resources/cars/Vehicle_400.jpg", car_name = "Landstalker", car_tuning = "Transfender" }); 
... ((about 300-500 others))

我用每个约100-200个项目填充4个列表框

解决方法:

这是我看到的问题:

>实际上,您的ListBox尚未虚拟化.设置ScrollViewer.CanContentScroll =“ False”会禁用虚拟化,而与您设置的其他属性无关.将其更改为True应该可以解决此问题.
>无需在ItemTemplate中使用VirtualizingStackPanel.由于每个面板中只有4个TextBlock,因此虚拟化将无法​​发挥作用,并且您只会增加不必要的开销.请改用StackPanel.

标签:performance,listbox,datatemplate,wpf,c
来源: https://codeday.me/bug/20191030/1966395.html