用代码如何给DataTemplate内的对象增加事件
定义一个DataTemplate:
<DataTemplate x:Key= "cjsRzfxInfoWindowTemplate ">
<Grid>
<HyperlinkButton Content= "详细... " Tag= "{Binding [projectid]} " Name= "firstDownLink "/>
</Grid>
</DataTemplate>
在后台使用代码如下:
private void getBtn()
{
DataTemplate pDT = Application.Current.Resources[m_sunInfoWindowTemplate] as DataTemplate;
Grid pGrid = pDT.LoadContent() as Grid;
HyperlinkButton pHBtn = pGrid.FindName( "firstDownLink ") as HyperlinkButton;//得到下载按钮
pHBtn.Click += new RoutedEventHandler(pHBtn_Click);//注册事件
}
private void pHBtn_Click(object sender, RoutedEventArgs e)
{
string projectid = Convert.ToString((sender as HyperlinkButton).Tag);
MessageBox.Show( "文件UUID: " + projectid);
}
为什么HyperlinkButton pHBtn的Click事件不能获得
[解决办法]
在xaml里写datatemplate的话,可以直接注册事件。
<DataTemplate x:Key="cjsRzfxInfoWindowTemplate">
<Grid>
<HyperlinkButton Content="详细..." Click="pHBtn_Click" Tag="{Binding [projectid]}" Name="firstDownLink"/>
</Grid>
</DataTemplate>