DataGridTemplateColumn CreateDataGridTemplateColumn() {
//创建DataGridTemplateColumn 对象, 设置Header等内容
DataGridTemplateColumn templateColumn = new DataGridTemplateColumn();
templateColumn.Header = "标题";
//创建 FrameworkElementFactory 对象, 设置Orientation属性水平排列
FrameworkElementFactory factory = new FrameworkElementFactory(typeof(StackPanel));
factory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
//向 FrameworkElementFactory 对象追加一个factory对象
FrameworkElementFactory buttonEdit = new FrameworkElementFactory(typeof(Button));
buttonEdit.SetValue(ContentProperty, "编辑");
factory.AppendChild(buttonEdit);
//创建DataTemplate对象, 设置VisualTree 值为factory
FrameworkElementFactory buttonDel = new FrameworkElementFactory(typeof(Button));
buttonDel.SetValue(ContentProperty, "删除");
factory.AppendChild(buttonDel);
//创建DataTemplate对象, 设置VisualTree 值为factory
DataTemplate dataTemplate = new DataTemplate();
dataTemplate.VisualTree = factory;
//最后把DataGridTemplateColumn 的CellTemplate 值设置为dataTemplate
templateColumn.CellTemplate = dataTemplate;
return templateColumn;
}
WPF怎么使用代码创建数据模板DataTemplate
最新推荐文章于 2023-12-28 14:28:33 发布