DataGridTemplateColumn col1 = new DataGridTemplateColumn();
col1.Header = "操作";
FrameworkElementFactory factory1 = new FrameworkElementFactory(typeof(Button));
//b1.Mode = BindingMode.TwoWay;
Binding b1 = new Binding("count");
//b1.Mode = BindingMode.
factory1.SetValue(Button.ContentProperty, "打印");
// factory1.SetValue(Button.DataContextProperty,b1);
factory1.AddHandler(Button.ClickEvent,new RoutedEventHandler(Button_Click_3));
DataTemplate cellTemplate1 = new DataTemplate();
cellTemplate1.VisualTree = factory1;
col1.CellTemplate = cellTemplate1;
datagrid.Columns.Add(col1);
下面是显示效果