我以编程方式生成了很多文本块。我想将onclick函数绑定到它们...如果我将textblock写入我的xaml代码很容易,但是如果以编程方式生成文本块,我该怎么办呢?
我到目前为止:
TextBlock tb1 = new TextBlock();
tb1.KeyDown += new System.Windows.Input.KeyEventHandler(MyTextBlock_KeyDown);
...
private void MyTextBlock_KeyDown(object sender, GestureEventArgs e) {
MessageBox.Show("hello world");
}
答案 0 :(得分:3)
public MainPage()
{
InitializeComponent();
TextBlock block = new TextBlock();
block.Text = "Tap me";
block.Tap += new EventHandler<GestureEventArgs>(block_Tap);
TitlePanel.Children.Add(block);
}
void block_Tap(object sender, GestureEventArgs e)
{
//Do something
}
这很好用......