GridViewColumn column = new GridViewColumn();
GridViewColumnHeader h = new GridViewColumnHeader();
h.Content = "??????";
h.Tag = "*******";
column.Header = h;
DataTemplate dt = new DataTemplate();
FrameworkElementFactory fef = new FrameworkElementFactory(typeof(CheckBox));
Binding binding = new Binding();
binding.Path = new PropertyPath("MarketIndicator");
fef.SetBinding(CheckBox.ContentProperty, binding);
fef.SetValue(CheckBox.ForegroundProperty, Brushes.White);
dt.VisualTree = fef;
column.CellTemplate = dt;
//-----------------------网上找的这段代码,是可以通过fef.SetValue来给CheckBox属性赋值,
但是如果想为上层控件添加事件,fef.SetValue(CheckBox.ClickEvent,?); ?位置放什么才对呢,显示是object可是放上我写的事件函数名,或者放委托都是错误呢???,求如何给上层元素添加事件??
DataGridTextColumn txtcl = null; Binding binding = null; DataTable student = ClassBLL.DormitoryBll.TableFeilds("V_Student");//获取视图字段 //-------------添加选择列 DataGridTemplateColumn dgtc = new DataGridTemplateColumn(); dgtc.Header = "选择"; DataTemplate dt = new DataTemplate(); //定义子元素 FrameworkElementFactory fef = new FrameworkElementFactory(typeof(CheckBox)); binding = new Binding("ID"); fef.SetValue(CheckBox.TagProperty, binding); fef.SetValue(CheckBox.ForegroundProperty, Brushes.Red); //为每个对象添加事件 fef.AddHandler(CheckBox.ClickEvent, new RoutedEventHandler(Row_Checked)); dt.VisualTree = fef;//添加子元素 dgtc.CellTemplate = dt; dgrid.Columns.Add(dgtc);//将第一列添加到数据表