简体   繁体   中英

Performance with WPF Combo Box inside a ListView

I was wondering if I was missing something obvious.

I have a simple window with a ListView with 3 columns in it. One displays text and the other two have combo boxes in them.

The ListView has approx. 500 records and the Comboboxes both pull from the same contact list which has approx. 8,000 records in it.

I am using MVVM.

This window takes for ever to open and once it does open it is practically frozen solid (it moves so slow)

the queries to the database take under ten seconds (I log when the VM is fully loaded) then it takes two or three minutes to open the window. I made sure to store both lists in List<T> in my VM to make sure its not reprocessing the data or anything like that.

As you can see below. I've tried explicitly using Virtualizing Stack Panel but that did not help much.

Thank you for any help

        <DataTemplate x:Key="ComboboxItemTemplate">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Image Grid.RowSpan="3" Source="{Binding ImageURL, IsAsync=True}" Width="50" />
            <TextBlock Grid.Column="1" Text="{Binding Name}" />
            <TextBlock Grid.Column="1" Grid.Row="1" Text="{Binding Email}" />
            <TextBlock Grid.Column="1" Grid.Row="2" Text="{Binding CampusName}" />
        </Grid>
    </DataTemplate>
    <ListView ItemsSource="{Binding MainList}" IsSynchronizedWithCurrentItem="True" Grid.RowSpan="2">
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel />
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
        <ListView.View>
            <GridView>
                <GridViewColumn Width="200" Header="Internal">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <TextBlock Text="{Binding Name}" FontWeight="Bold" />
                                <TextBlock Text="{Binding MName}" />
                                <TextBlock Text="{Binding CampusName}" />
                            </StackPanel>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Width="200" Header="Contact1">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding Source={StaticResource VM}, Path=ContactList, IsAsync=True}" SelectedValue="{Binding HisContactID}" SelectedValuePath="id" ItemTemplate="{StaticResource ComboboxItemTemplate}" Background="{Binding HisColor}" Margin="0,82,0,115" Grid.Row="1" Grid.Column="1">
                                <ComboBox.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <VirtualizingStackPanel />
                                    </ItemsPanelTemplate>
                                </ComboBox.ItemsPanel>
                            </ComboBox>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Width="200" Header="Contact2">
                ...
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>

I had the same issue and finally figured it out...

It happens that the ListView was nested inside an Infragistic TabControl and each time something was bound inside the ListView (ie: ComboBoxes), the "SelectionChange" of the TabControl was firing, causing the delay...

I've also tested with a native Microsft TabControl and I got fairly the same behavior, but somewhat a bit more performant.

I solved the issue by validating the SelectionChangedEventArgs... making sure the e.AddedItems contains only "TabItem" (and not ComboBoxes) before processing.

Hope it helps,

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