c# – 以编程方式在WPF中创建一个网格作为模板

我想用编程方式创建一个基本的用户控件.
在这种风格我想添加一个网格(没有问题),但我不能添加列定义到这个网格.

我的示例代码

ControlTemplate templ = new ControlTemplate();
FrameworkElementFactory mainPanel = new FrameworkElementFactory(typeof(DockPanel));
mainPanel.SetValue(DockPanel.LastChildFillProperty,true);

FrameworkElementFactory headerPanel = new FrameworkElementFactory(typeof(StackPanel));
headerPanel.SetValue(StackPanel.OrientationProperty,Orientation.Horizontal);
headerPanel.SetValue(DockPanel.DockProperty,Dock.Top);
mainPanel.AppendChild(headerPanel);

FrameworkElementFactory headerImg = new FrameworkElementFactory(typeof(Image));
headerImg.SetValue(Image.MarginProperty,new Thickness(5));
headerImg.SetValue(Image.HeightProperty,32d);
headerImg.SetBinding(Image.SourceProperty,new Binding("ElementImage") { RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent) });
headerPanel.AppendChild(headerImg);

FrameworkElementFactory headerTitle = new FrameworkElementFactory(typeof(TextBlock));
headerTitle.SetValue(TextBlock.FontSizeProperty,16d);
headerTitle.SetValue(TextBlock.VerticalAlignmentProperty,VerticalAlignment.Center);
headerTitle.SetBinding(TextBlock.TextProperty,new Binding("Title") { RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent) });
headerPanel.AppendChild(headerTitle);

FrameworkElementFactory mainGrid = new FrameworkElementFactory(typeof(Grid));
FrameworkElementFactory c1 = new FrameworkElementFactory(typeof(ColumnDefinition));
c1.SetValue(ColumnDefinition.WidthProperty,new GridLength(1,GridUnitType.Star));
FrameworkElementFactory c2 = new FrameworkElementFactory(typeof(ColumnDefinition));
c2.SetValue(ColumnDefinition.WidthProperty,GridUnitType.Auto));
FrameworkElementFactory c3 = new FrameworkElementFactory(typeof(ColumnDefinition));
c3.SetValue(ColumnDefinition.WidthProperty,new GridLength(3,GridUnitType.Star));
FrameworkElementFactory colDefinitions = new FrameworkElementFactory(typeof(ColumnDefinitionCollection));
colDefinitions.AppendChild(c1);
colDefinitions.AppendChild(c2);
colDefinitions.AppendChild(c3);
mainGrid.AppendChild(colDefinitions);

mainPanel.AppendChild(mainGrid);

FrameworkElementFactory content = new FrameworkElementFactory(typeof(ContentPresenter));
content.SetBinding(ContentPresenter.ContentProperty,new Binding() { RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent),Path = new PropertyPath("Content") });
mainGrid.AppendChild(content);

templ.VisualTree = mainPanel;
Style mainStyle = new Style();
mainStyle.Setters.Add(new Setter(UserControl.TemplateProperty,templ));
this.Style = mainStyle;

但是类型为ColumnDefinitionCollection的FrameworkElementFactory的创建将抛出异常“”ColumnDefinitionCollection“类型必须从FrameworkElement,FrameworkContentElement或Visual3D派生.

谁能帮我?

解决方法

FrameworkElementFactory有一些自定义逻辑来处理Grid中的ColumnDefinitions和RowDefinitions.对于这些价值观,您可以像工厂树中的孩子一样对待他们,例如:
FrameworkElementFactory gridFactory = new FrameworkElementFactory(typeof(Grid));
var column1 = new FrameworkElementFactory(typeof(ColumnDefinition));
                column1.SetValue(ColumnDefinition.WidthProperty,GridUnitType.Auto));
                var column2 = new FrameworkElementFactory(typeof(ColumnDefinition));
                column2.SetValue(ColumnDefinition.WidthProperty,GridUnitType.Star));

gridFactory.AppendChild(column1);
gridFactory.AppendChild(column2);

相关文章

在项目中使用SharpZipLib压缩文件夹的时候,遇到如果目录较深,则压缩包中的文件夹同样比较深的问题。比...
项目需要,几十万张照片需要计算出每个照片的特征值(调用C++编写的DLL)。 业务流程:选择照片...
var array = new byte[4]; var i = Encoding.UTF8.GetBytes(100.ToString("x2"));//...
其实很简单,因为Combox的Item是一个K/V的object,那么就可以把它的items转换成IEnumerable<Dic...
把.net4.6安装包打包进安装程序。 关键脚本如下: 头部引用字符串对比库 !include "WordFunc....
项目需求(Winform)可以批量打印某个模板,经过百度和摸索,使用iTextSharp+ZXing.NetʿreeSp...