3

我有一个ListBox有一个DataTemplate. 这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();
    }

初始化Rsc:

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 个列表框

4

1 回答 1

4

以下是我看到的问题:

  1. 您的 ListBox 实际上没有被虚拟化。ScrollViewer.CanContentScroll="False"无论您设置的其他属性如何,设置都会禁用虚拟化。将其更改为True应该可以解决此问题。
  2. 无需VirtualizingStackPanel在您的ItemTemplate. 虚拟化不会发挥作用,因为每个面板中只有 4 个 TextBlock,而且您只是增加了不必要的开销。改为使用StackPanel
于 2013-11-03T10:09:15.797 回答