开发者

Why is my ComboBox taking so long to drop down when I run my program from within Visual Studio?

In my view model, I have two related properties. Their implementations look like this:

public string Code
{
   get { return _Code; }
   set
   {
      if (_Code != value)
      {
         _Code = value;
         OnPropertyChanged("Code");
         OnPropertyChanged("RelatedCodeList");
      }
}

public List<Code> RelatedCodeList
{
   get
   {
      return CodeLists[Code];
   }
}

CodeLists is a Dictionary<string, List<Code>>.

I have a ComboBox that's bound to RelatedCodeList; its implementation looks like this:

<ComboBox SelectedItem="{Binding RelatedCode, Mode=TwoWay}"
          ItemsSource="{Binding RelatedCodeList}">

This seems simple enough, and it works, except for one thing. When I change Code in the UI and then click on the combo box, it takes two or three seconds to drop down. Even if the list has less than 10 items in it.

What could cause this? It's not collection-change events: List doesn't implement INotifyCollectionChanged, and anyway the collection isn't changing. It doesn't seem to be happening inside the view model;开发者_如何学JAVA I've put breakpoints into the property getters, and the getters break as soon as the property-change events are raised; they don't get called when the combo box is dropping down. It doesn't seem to be the item rendering: there's no data template defined for the Code class, and the implementation of ToString() in that class is trivial.

What am I overlooking?

Edit

I followed up on Will's suggestion, and found that this doesn't happen unless I'm running under the debugger. If I just run the executable (even the Debug build), it performs just fine.

So in an effort to make this question actually useful, let's reword it: Why does this happen? And, more importantly, if the debugger is to blame, is there any way for me to tell that the debugger is bollixing things up, so that I don't spend hours trying to find the cause of problems that don't actually exist?


This type of question is really tough to answer because you haven't really given enough information. Nothing that you've supplied could be causing the issue.

But using my amazing powers of deduction, I'm going to suggest that you check that you don't have any circular OnPropertyChanged calls happening - I've noticed that these can stall the debugger but go almost unnoticed otherwise.

Otherwise, check your CodeLists indexer.


I found that WPF ComboBox is lacking of Virutal Stack Panel and it makes rendering slow at UI. Including Virtual Stack Panel is a perfect solution for this.

Include Following into your window / user control resources

<ItemsPanelTemplate x:Key="VSP">
    <VirtualizingStackPanel/>
</ItemsPanelTemplate>

Update ComboBox XAML -

<ComboBox ItemsSource="{Binding Path=MyDataSource}"  DisplayMemberPath="Name" SelectedValuePath="Id" ItemsPanel="{StaticResource VSP}"/>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜