这次由于实现的原因,需要将这一事件绑定的过程通过Code Behind的方式来绑定。
实现原理:
借助DataGrid的LoadingRow事件,在初始化DataGrid过程中,使用GetCellContent方法,来得到某一列的控件。
关键代码:
private void grd_LoadingRow(object sender, DataGridRowEventArgs e) { Button btnUpdateNum = this.grd.Columns[4].GetCellContent(e.Row).FindName("btnUpdateNum") as Button; btnUpdateNum.Click += new RoutedEventHandler(btnUpdateNum_Click); } private void btnUpdateNum_Click(object sender, RoutedEventArgs e) { //TODO something }
举一反三:
了解到DataGrid的LoadingRow事件后,在这里面也可以追加对控件的一些特殊控制的代码。比如得到该行DataContext后,根据
属性值来控制控件的内容或显示等等其他属性,替代一些需要用Converter来做的事情。
最新评论