想要在数据模板中的按钮上添加点击事件。有一些代码:
var temp = (DataTemplate)XamlReader.Load(
new MemoryStream(Encoding.Default.GetBytes(
@"<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'><Button><TextBlock Text='1' TextAlignment='Center'/></Button></DataTemplate>"
)));
var button = temp.LoadContent() as Button;
button.Click += (sender, args) =>
{
MessageBox.Show("123");
};
return temp;
所以,当我点击按钮时,什么也没有发生。我错过了什么?