简体   繁体   English

WPF ComboBox绑定的奇怪性能问题

[英]Strange performance issue with WPF ComboBox binding

I have a performance issue that I just solved but I really don't understand why the solution works. 我刚刚解决了一个性能问题,但我真的不明白为什么该解决方案有效。

I have a ComboBox with about 4,000 items that I bind to a collection using the ItemSource property; 我有一个包含约4,000个项目的ComboBox,我使用ItemSource属性将其绑定到集合; if I bind to a property in the view-model with a getter and a setter everything works fine, but if I bind to a property with only a getter, the first time that I click on the combobox it works fine but everytime after that first time if I click on the combobox the application hangs for about 1 minute with the CPU for the process at ~100% before displaying the combo box items 如果我使用getter和setter绑定到视图模型中的属性,则一切正常,但是如果我仅使用getter绑定到属​​性,则第一次单击组合框时,它会正常运行,但是每次之后时间,如果我单击组合框,则在显示组合框项目之前,应用程序挂起约1分钟,进程的CPU占用率约为100%

View: 视图:

...
<ComboBox
    Grid.Column="1"
    ItemsSource="{Binding AvailableDispositionCodes}"
    DisplayMemberPath="DisplayName"
    SelectedItem="{Binding SelectedDispositionCode}"
    Width="Auto"
    Height="25"
    Margin="5 0 0 0">
    <ComboBox.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel />
        </ItemsPanelTemplate>
    </ComboBox.ItemsPanel>
</ComboBox>
...

Working view-model: 工作视图模型:

...
private IEnumerable<DispositionCodeViewModel> availableDispositionCodes = new List<DispositionCodeViewModel>();
...
public IEnumerable<DispositionCodeViewModel> AvailableDispositionCodes
{
    get
    {
        return this.availableDispositionCodes;
    }

    set
    {
        this.availableDispositionCodes = value;
        this.OnPropertyChanged();
    }
}
...
public void Initialize()
{
    ...
    this.AvailableDispositionCodes = resultCodeViewModels.OrderBy(x => x.Name);
    ...
}
...

View-model that causes the application to hang: 导致应用程序挂起的视图模型:

...
private List<DispositionCodeViewModel> availableDispositionCodes = new List<DispositionCodeViewModel>();
...
public IEnumerable<DispositionCodeViewModel> AvailableDispositionCodes
{
    get { return this.availableDispositionCodes; }
}
...
public void Initialize()
{
    ...
    this.availableDispositionCodes.AddRange(resultCodeViewModels.OrderBy(x => x.Name));
    this.OnPropertyChanged(nameof(this.AvailableDispositionCodes));
    ...
}
...

The method Initialize of the view-model initializes the collection that is binded to the combobox and this method is called just once shortly after the view and the view-model are created. 视图模型的Initialize方法初始化绑定到组合框的集合,并且在创建视图和视图模型之后不久,将立即调用此方法一次。 After that the collection doesn't change 之后,收藏不会改变

Does anybody knows what causes this weird behavior? 有人知道导致这种奇怪行为的原因吗?

I think it is about the List.AddRange() rather than the property. 我认为这是关于List.AddRange()而不是属性。

If the new Count (the current Count plus the size of the collection) will be greater than Capacity, the capacity of the List is increased by automatically reallocating the internal array to accommodate the new elements, and the existing elements are copied to the new array before the new elements are added. 如果新的Count(当前Count加上集合的大小)将大于Capacity,则通过自动重新分配内部数组来容纳新元素来增加List的容量,并将现有元素复制到新数组中在添加新元素之前。

msdn msdn

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM