我有一个问题,我有一些动态按钮。我想存储整数值。
并在该按钮的单击事件上获取该值,我该如何实现它
提前谢谢 沙善DataView dv = new DataView(dtCat, "PK_CATEGORY_ID IN(" + categoryIds.ToString() + "0)", "PK_CATEGORY_ID", DataViewRowState.CurrentRows);
foreach (DataRowView rr in dv)
{
//MessageBox.Show(rr[0].ToString() + "------------" + rr[1].ToString());
Button b2 = new Button();
//b2.Name = rr[0].ToString();
b2.Name = "";
b2.Height = 200;
b2.Width = 200;
b2.Margin = new Thickness(0, -100, 0, 0);
b2.HorizontalAlignment = HorizontalAlignment.Left;
b2.Content = rr[1].ToString();
b2.Background = System.Windows.Media.Brushes.Pink;
b2.Click += new RoutedEventHandler(b2_Click);
btncanvas.Children.Add(b2);
Canvas.SetLeft(b2, b2.Width * i);
i = i + 1;
MessageBox.Show(rr[0].ToString());
b2.Tag = rr[0].ToString();
}
void b2_Click(object sender, RoutedEventArgs e)
{
Button clicked = (Button)sender;
categoryname = clicked.Name;
}
答案 0 :(得分:3)
Tag
属性可能就是你想要的。
您已在示例中使用它,但只有:
b2.Tag = integerValue;
然后在您的点击处理程序中使用Convert.ToInt32(object)
方法返回整数值:
int retrievedValue = Convert.ToInt32(clicked.Tag);