C# 在网格中添加动态按钮
我想在网格上添加225(15x15)个按钮。我创建了行和列。然后我创建了动态按钮。我试着这样做 MainWindow.xamlC# 在网格中添加动态按钮,c#,wpf,xaml,C#,Wpf,Xaml,我想在网格上添加225(15x15)个按钮。我创建了行和列。然后我创建了动态按钮。我试着这样做 MainWindow.xaml <Window x:Class="WpfApplication15.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
<Window x:Class="WpfApplication15.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="500" Width="500">
<Grid Name="gridMain" ShowGridLines="True" >
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
</Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication15
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 15; j++)
{
Button MyControl = new Button();
MyControl.Content = i.ToString();
MyControl.Name = "Button" + i.ToString();
gridMain.SetValue(Grid.RowProperty, j);
gridMain.Children.Add(MyControl);
}
gridMain.SetValue(Grid.ColumnProperty, i);
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Button b = (Button)sender;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间WpfApplication15
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
对于(int i=0;i<15;i++)
{
对于(int j=0;j<15;j++)
{
按钮MyControl=新建按钮();
MyControl.Content=i.ToString();
MyControl.Name=“Button”+i.ToString();
SetValue(Grid.RowProperty,j);
gridMain.Children.Add(MyControl);
}
SetValue(Grid.ColumnProperty,i);
}
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
按钮b=(按钮)发送器;
}
}
}
但我看到:
当我想看这个的时候:
谢谢
我解决了这个问题
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication15
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
int count = 1;
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 15; j++)
{
Button MyControl1 = new Button();
MyControl1.Content = count.ToString();
MyControl1.Name = "Button" + count.ToString();
Grid.SetColumn(MyControl1, j);
Grid.SetRow(MyControl1, i);
gridMain.Children.Add(MyControl1);
count++;
}
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Button b = (Button)sender;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间WpfApplication15
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
整数计数=1;
对于(int i=0;i<15;i++)
{
对于(int j=0;j<15;j++)
{
按钮MyControl1=新按钮();
MyControl1.Content=count.ToString();
MyControl1.Name=“Button”+count.ToString();
Grid.SetColumn(MyControl1,j);
Grid.SetRow(MyControl1,i);
gridMain.Children.Add(MyControl1);
计数++;
}
}
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
按钮b=(按钮)发送器;
}
}
}
一切正常,只需使用Grid类设置列和行,即“Grid.SetRow(someLabel,0);”@温梅尔给出了完美的答案谢谢,我解决了这个问题