【问题标题】:Click event for programatically generated XAML buttons以编程方式生成的 XAML 按钮的单击事件
【发布时间】:2015-07-23 04:16:22
【问题描述】:

我正在以编程方式生成一个表格,其中 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.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);
} 

如何以编程方式添加点击事件?当那个点击事件被执行时,我怎么知道它来自哪个按钮?

【问题讨论】:

  • 将命令附加到您的按钮并绑定到命令...否则添加点击处理程序。它应该与您通常的做法没有什么不同。

标签: c# xaml


【解决方案1】:

向 Text2.Click 添加一个事件处理程序,它将发送者作为参数传递,这样您就知道点击了哪个。

【讨论】:

    猜你喜欢
    • 2013-05-02
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    • 2013-10-09
    • 1970-01-01
    • 2011-09-03
    • 2017-06-21
    • 1970-01-01
    相关资源
    最近更新 更多