如何创建一个包含板动态网格 [英] How to create a dynamic grid containing panels
问题描述
我没有要求来动态地创建一个WPF网格具有3行和列的数目将取决于面板我需要的量。第一个小组将在网格位置0,0接下来的0,1 - > 0,2 - > 1,0 - > 1,1等。所以,如果我需要20板电网将有3议和7行。与电网应该充满整个窗口
I have a requirement to dynamically create a WPF Grid that has 3 columns and the number of rows will depend on the amount of panels I need. The first panel will be in grid location 0,0 the next in 0,1 -> 0,2 -> 1,0 -> 1,1 etc.. So if I needed 20 panels the grid would have 3 cols and 7 rows. And the Grid should fill the whole window
和每个小组应该有一个标题,并在其上的进度条。我想我有我的面板作为一个单独的用户控件
And each panel should have a heading and a progress bar on it. I guess I would have my panel as a seperate user control
我开始用WPF所以任何帮助将是巨大的。
I'm starting out with WPF so any help would be great
多谢
推荐答案
下面的示例演示如何创建行数和列数,并把对象(在这种情况下,标签)到每个细胞中。看看你是否可以从那里推断。
The following example shows how you can create the number of rows and columns and put objects (in this case a Label) into each cell. See if you can extrapolate from there.
public partial class MainWindow : Window
{
Grid grid1;
public MainWindow()
{
InitializeComponent();
int cellCount = 14;
int numCols = 3;
int numRows = (cellCount + 1) / numCols;
grid1 = new Grid();
this.AddChild(grid1);
for(int i=0; i<numCols; ++i)
this.grid1.ColumnDefinitions.Add(new ColumnDefinition());
for (int i = 0; i < numRows; ++i)
this.grid1.RowDefinitions.Add(new RowDefinition());
foreach (var g in this.grid1.RowDefinitions)
{
g.Height = new GridLength(100);
}
foreach (var g in grid1.ColumnDefinitions)
{
g.Width = new GridLength(100);
}
for(int i=0; i<cellCount; ++i)
{
int idx = grid1.Children.Add(new Label());
Label x = grid1.Children[idx] as Label;
x.Content = "Cell " + i;
x.SetValue(Grid.RowProperty, i/numCols);
x.SetValue(Grid.ColumnProperty, i % numCols);
}
}
}
这个例子开始于一个几乎全空的XAML。它所拥有的窗口元素。
This examples starts with a nearly completely empty XAML. All it has is the Window element.
这篇关于如何创建一个包含板动态网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!