使用 WPF 在可滚动区域中创建数千个控件的最佳方式是什么

发布于 2024-07-25 13:40:34 字数 425 浏览 3 评论 0原文

我正在使用 WPF 和 C#。

我有个问题。 我需要在可滚动区域中创建很多可绑定模板化控件(它们都是不同类型的)。 例如 1000 个文本框、1000 个下拉列表和 1000 个复选框。

问题是,当它们全部创建时,它的运行速度非常慢。

问题是 - 是否可以同时拥有如此多的控件而没有滞后,我应该朝哪个方向寻找解决方案? 我想保留使用模板、数据绑定和简单事件处理(如鼠标单击)的能力。

PS我尝试了很多东西(仅渲染几何图形,不同的基类等等),但目前似乎对我有用的唯一方法(我还没有尝试过)是仅创建那些适合当前视口的控件并更新此滚动控件列表。

PPS 我知道 3.5 SP1 中有一种机制,当列表项在滚动期间重用时,在 ListBox 中使用,但这种方法不能在这里使用,因为所有这些项都是不同类型的。

多谢。

I'm using WPF and C#.

I have a problem. I need to create a lot of bindable templated controls in scrollable area (they are all of different types). For example 1000 textboxes, 1000 drop down lists, and 1000 checkboxes.

The problem is that when they are all created it works really slow.

The question is - whether it is possible to have so many controls simultaniously without lags and in what direction should I search for the solution? I'd like to save an ability to use Templates, Databinding and simple event handling like mouse clicking.

P.S. I tried lot's of things (rendering of geometry only, different base classes and so on) but currently the only approach that seems working for me (i haven't tried it yet) is to create only those controls that fit into the current viewport and update this list of controls on scrolling.

P.P.S I know that there is a mechanism in 3.5 SP1 that is used in ListBox when list items are reused during scrolling but such approach cannot be used here because all those items are of different types.

Thank's a lot.

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

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

发布评论

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

评论(2

烟雨扶苏 2024-08-01 13:40:34

事实上,您希望在一个屏幕上有大约 3000 个控件,这是一个问题,这通常表明您从错误的角度接近 GUI。 我对 WPF 的经验不是很丰富,但我的 WinForms 知识告诉我,1000 多个控件实例只是自找麻烦。 这就是列表框、数据网格和其他表格/列表格式控件的设计目的。

我在这里对类似问题给出了类似的答案: Super Slow C# Custom Control

也许如果你解释一下你想做什么您正在尝试实施什么解决方案,这里有人可以将您推向正确的方向。

The fact that you want ~3000 controls on a single screen is a problem and it is usually a sign that you are approaching you GUI from the wrong angle. I am not very experienced with WPF, but my WinForms knowledge tells me that 1000+ control instances is just asking for trouble. This is what ListBoxes, DataGrids, and other tabular/list format controls were designed for.

I gave a similar answer to a similar problem here: Super Slow C# Custom Control

Maybe if you explain what are you trying to do instead of what solution you are trying to implement someone here can push you in the right direction.

注定孤独终老 2024-08-01 13:40:34

首先 - 我会质疑此类需求的设计原理。 除了网格或某种列表之外的任何内容中拥有数千个控件(它们都是恒定的,不会改变,因此列表样式的方法可以正常工作)会让您的用户感到非常困惑。 你基本上是在强迫一个不明显的、不断变化的界面,其中包含数千个项目。

话虽如此...

PPS 我知道 3.5 SP1 中有一种机制,当列表项在滚动期间重用时,在 ListBox 中使用,但这种方法不能在这里使用,因为所有这些项都是不同类型的。

这实际上可能是您最好的方法。 这应该可以正常工作 - 尽管它不像列表框机制那么简单。 您始终可以预先创建控件 - 只需根据需要创建足够的每种类型的控件来填充当前容器,然后在滚动时将适当的控件重新分配到适当的位置。

如果您确实需要数千个控件,这很可能是您的最佳选择。

First - I'd question the rationale in design of this type of requirement. Having thousands of controls in anything except a grid or list of some sort (where they are all constant, not changing, so the list-style approach would work fine) is going to be incredibly confusing to your user. You're basically forcing a non-obvious, changing interface with thousands of items at them.

That being said...

P.P.S I know that there is a mechanism in 3.5 SP1 that is used in ListBox when list items are reused during scrolling but such approach cannot be used here because all those items are of different types.

This actually is probably your best approach. This should work fine - although it won't be as simple as the list box mechanism. You can always pre-create the controls - just create enough of each type of control as required to fill the current container, and as you scroll, reassign the appropriate controls into the appropriate locations.

If you truly need thousands of controls, this will most likely be your best option.

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