Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 将大量按钮加载到WrapPanel中的最佳方法_Wpf_Load_Wrappanel - Fatal编程技术网

Wpf 将大量按钮加载到WrapPanel中的最佳方法

Wpf 将大量按钮加载到WrapPanel中的最佳方法,wpf,load,wrappanel,Wpf,Load,Wrappanel,我想知道是否有可能将~70个按钮加载到WrapPanel中,这是我现在使用的更好的方式 此时,它的工作原理如下: 我在DB中有一个表,列:productID,productName(它们代表要加载的70个按钮,所以总是有70行) 现在在循环中,使用SqlDataReader我正在阅读: product.price (from table Product), barcode (from table Barcode) where Product.productID = myTableWithButt

我想知道是否有可能将~70个按钮加载到
WrapPanel
中,这是我现在使用的更好的方式

此时,它的工作原理如下:

我在DB中有一个表,列:
productID
productName
(它们代表要加载的70个按钮,所以总是有70行)

现在在循环中,使用
SqlDataReader
我正在阅读:

product.price (from table Product), barcode (from table Barcode) where Product.productID = myTableWithButtons.productID
如果存在,则:

使用类创建按钮

returnButton(int productID, string picturePath, decimal price)
我返回从
WrapPanel
创建的按钮,其中有两个文本块和(如果存在)图片。 如果不存在-插入没有任何文本等的空按钮,并将其设置为不可见

现在测试: 在具有4 gb ram的core2 pc中,加载时间为0.91秒 在D525处理器中,2gb ram的加载时间为4-5秒

有没有办法将按钮加载并插入到
WrapPanel
? 也许一些
列表
会更快

每个按钮在资源文件中都有样式-渐变笔刷、
WrapPanel
textBlocks
作为内容等

谢谢你的回复!
关于

使用
ItemsControl
,将
ItemsControl.ItemsPanel
更改为
WrapPanel
,用SQL填充
DataTable
,并将其绑定到
ItemsControl.ItemsSource
,添加模板,WPF将为其创建所有按钮you@dkozl比我快。这正是解决问题的方法。快速简单地解释答案。谢谢,我会检查:)顺便说一句,我需要做一些事情,就像你说的那样,但是使用静态Listview。这样,我需要在codebehind中编写所有ItemTemplate。你能帮我举个例子吗?@user13657为什么要在代码隐藏中创建控件?静态是什么意思?为什么不使用XAML?你想要实现什么?