WPF怎么使用代码创建数据模板DataTemplate

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_编程网

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值