Create a grid in WPF as Template programmatically

前端 未结 4 1879
太阳男子
太阳男子 2021-01-17 13:59

I want to create a basic user control with a style programmatically. In this style I want to add a Grid (no problem), but I can\'t add column definitions to thi

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-17 14:49

    //create grid 
                var grid = new FrameworkElementFactory(typeof(Grid));
    
                // assign template to grid 
                CellControlTemplate.VisualTree = grid;
    
                // define grid's rows 
                var r = new FrameworkElementFactory(typeof(RowDefinition));
                grid.AppendChild(r);
    
                // define grid's columns
                var c = new FrameworkElementFactory(typeof(ColumnDefinition));
                grid.AppendChild(c);
    
                c = new FrameworkElementFactory(typeof(ColumnDefinition));
                c.SetValue(ColumnDefinition.WidthProperty, GridLength.Auto);
                grid.AppendChild(c);
    
                c = new FrameworkElementFactory(typeof(ColumnDefinition));
                c.SetValue(ColumnDefinition.WidthProperty, GridLength.Auto);
                grid.AppendChild(c);
    

提交回复
热议问题