C# 单击以编程方式生成的XAML按钮的事件
我正在通过编程生成一个表,其中Text2列是一个按钮:C# 单击以编程方式生成的XAML按钮的事件,c#,xaml,C#,Xaml,我正在通过编程生成一个表,其中Text2列是一个按钮: for (int i = 0; i < workPackages.Length; i++) { //Define new Row to add RowDef = new RowDefinition(); RowDef.Height = new GridLength(60); //Add row definition to Grid WorkPackageResults.RowDefinitio
for (int i = 0; i < workPackages.Length; i++)
{
//Define new Row to add
RowDef = new RowDefinition();
RowDef.Height = new GridLength(60);
//Add row definition to Grid
WorkPackageResults.RowDefinitions.Add(RowDef);
//Define the control that will be added to new row
Text1 = new TextBlock();
Text1.Text = workPackages[i].EWPStatus;
Text1.Width = 100;
Text1.TextAlignment = TextAlignment.Center;
Text2 = new Button();
Text2.Content = workPackages[i].EWPCode;
Text2.Width = 300;
Text3 = new TextBlock();
Text3.Text = workPackages[i].Description;
Text3.Width = 500;
Text3.TextAlignment = TextAlignment.Center;
Text4 = new TextBlock();
Text4.Text = workPackages[i].ForeBadge;
Text4.Width = 100;
Text4.TextAlignment = TextAlignment.Center;
//create stackpanel and define which row to add the stackpanel to
StackP = new StackPanel();
StackP.SetValue(Grid.RowProperty, i);
StackP.Orientation = Orientation.Horizontal;
StackP.Margin = new Thickness(50, 0, 0, 0);
//add your control to the stackpanel
StackP.Children.Add(Text1);
StackP.Children.Add(Text2);
StackP.Children.Add(Text3);
StackP.Children.Add(Text4);
//add the stackpanel to the grid
WorkPackageResults.Children.Add(StackP);
}
for(int i=0;i
如何以编程方式添加单击事件?当点击事件被执行时,我如何知道它来自哪个按钮?向Text2添加一个事件处理程序。点击它会将发送者作为一个参数传递,这就是你知道哪个按钮被点击的方式。将命令附加到按钮并绑定到命令。。。否则,添加单击处理程序。这应该和你通常的做法没有什么不同。