[英]WPF fill stack panel with buttons?
I am creating buttons during runtime, calculating the size of StackPanel and dividing with button size and then adding buttons to the StackPanel. 我在运行时创建按钮,计算StackPanel的大小,然后除以按钮大小,然后将按钮添加到StackPanel。
Now I would like final result to look like this: 现在,我希望最终结果如下所示:
********
**BBBB**
**BBBB**
**BBBB**
********
Now, with StackPanel i am getting this: 现在,有了StackPanel,我得到了这个:
********
** **
**BBBB**
** **
********
Is StackPanel proper control to do this kind of work? 是StackPanel适当的控件来执行这种工作吗?
Code below is for another StackPanel (which works) but buttons are added verticaly: 下面的代码用于另一个StackPanel(有效),但是按钮是垂直添加的:
*****
**B**
**B**
**B**
*****
This is in Program.cs 这在Program.cs中
private void addButtonGrp()
{
//Calculate size of container to determine number of buttons
int btnMinimumHeightSize = 30;
int btnNumberCreated = (Convert.ToInt16(rctGrupe.Height) / btnMinimumHeightSize);
for (int i = 0; i < btnNumberCreated ; i++)
{
CreateGroupButtons btn = new CreateGroupButtons();
var btnX = new Button();
btnX=(btn.addButton(i,btnMinimumHeightSize,Convert.ToInt16(stPanel.Width)));
btnX.Click += ClickHandler2;
if (i==btnNumberCreated -1 )
{
btnX.Height = btnMinimumHeightSize + ((Convert.ToDouble(rctGrupe.Height) / btnMinimumHeightSize) % 1)*(btnNumberCreated);
}
stPanel.Children.Add(btnX);
}
}
And this is in class CreateGroupButtons 这是在类CreateGroupButtons中
public Button addButton(int num, int btnHeight, int btnWidth)
{
var btnX = new Button { Content = "Group " + num, Tag = num, Height = btnHeight, Width=btnWidth };
return btnX;
}
Here is a basic example of filling a UniformGrid with a 4x3 grid of Buttons: 这是一个用4x3的Button网格填充UniformGrid的基本示例:
<ItemsControl x:Name="buttonGrid">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="4"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding}" ContentStringFormat="Group {0}"
Click="ClickHandler2"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Set the ItemsSource property to an IEnumerable<int>
like this: 将ItemsSource属性设置为
IEnumerable<int>
如下所示:
buttonGrid.ItemsSource = Enumerable.Range(1, 12);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.