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

时间:2022-05-16 15:54:38

I'm using WPF and C#.

我正在使用WPF和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.

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

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.

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

Thank's a lot.

非常感谢。

2 个解决方案

#1


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.

您希望在单个屏幕上显示~3000个控件的事实是一个问题,这通常表明您从错误的角度接近GUI。我对WPF不是很有经验,但我的WinForms知识告诉我,1000多个控件实例只是在寻找麻烦。这就是ListBoxes,DataGrids和其他表格/列表格式控件的设计目标。

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

我在这里给出了类似问题的类似答案:超慢C#自定义控制

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.

也许如果你解释你想要做什么而不是你试图实现什么解决方案,那么这里的人可以推动你朝着正确的方向前进。

#2


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.

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

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.

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

#1


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.

您希望在单个屏幕上显示~3000个控件的事实是一个问题,这通常表明您从错误的角度接近GUI。我对WPF不是很有经验,但我的WinForms知识告诉我,1000多个控件实例只是在寻找麻烦。这就是ListBoxes,DataGrids和其他表格/列表格式控件的设计目标。

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

我在这里给出了类似问题的类似答案:超慢C#自定义控制

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.

也许如果你解释你想要做什么而不是你试图实现什么解决方案,那么这里的人可以推动你朝着正确的方向前进。

#2


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.

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

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.

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