当我从 Visual Studio 中运行程序时,为什么我的 ComboBox 需要很长时间才能下拉?

发布于 2024-12-02 06:26:57 字数 1264 浏览 1 评论 0 原文

在我的视图模型中,我有两个相关的属性。它们的实现如下所示:

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 是一个 Dictionary>

我有一个绑定到 RelatedCodeListComboBox;它的实现看起来像这样:

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

这看起来很简单,而且它有效,除了一件事之外。当我在 UI 中更改 Code 然后单击组合框时,需要两三秒才能下拉。即使列表中的项目少于 10 项。

什么可能导致这种情况?这不是集合更改事件:List 未实现 INotifyCollectionChanged,并且无论如何集合都不会更改。它似乎没有发生在视图模型内部;我已将断点放入属性获取器中,一旦引发属性更改事件,获取器就会中断;当组合框下拉时,它们不会被调用。这似乎不是项目渲染的问题:没有为 Code 类定义数据模板,并且该类中 ToString() 的实现很简单。

我在忽略什么?

编辑

我按照威尔的建议进行了跟进,发现除非我在调试器下运行,否则不会发生这种情况。如果我只是运行可执行文件(甚至是调试版本),它的性能就很好。

因此,为了使这个问题真正有用,让我们重新表述一下:为什么会发生这种情况?而且,更重要的是,如果调试器是罪魁祸首,我有什么办法可以告诉调试器正在把事情搞砸,这样我就不会花几个小时试图找到实际上不存在的问题的原因吗?

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; 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谢绝鈎搭 2024-12-09 06:26:57

这种问题很难回答,因为你没有提供足够的信息。您提供的任何内容都不会导致该问题。

但是利用我惊人的推论能力,我建议您检查一下是否发生了任何循环的 OnPropertyChanged 调用 - 我注意到这些调用可能会导致调试器停止运行,但否则几乎不会被注意到。

否则,请检查您的 CodeLists 索引器。

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.

蓬勃野心 2024-12-09 06:26:57

我发现 WPF ComboBox 缺少 Virtual Stack Panel,导致 UI 渲染速度变慢。虚拟堆栈面板是一个完美的解决方案。

将以下内容包含到您的窗口/用户控件资源中

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

更新 ComboBox XAML -

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

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}"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文