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

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

概述我想用编程方式创建一个基本的用户控件. 在这种风格我想添加一个网格(没有问题),但我不能添加列定义到这个网格. 我的示例代码是 ControlTemplate templ = new ControlTemplate();FrameworkElementFactory mainPanel = new FrameworkElementFactory(typeof(DockPanel));mainP 我想用编程方式创建一个基本的用户控件.
在这种风格我想添加一个网格(没有问题),但我不能添加列定义到这个网格.

我的示例代码是

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);
总结

以上是内存溢出为你收集整理的c# – 以编程方式在WPF中创建一个网格作为模板全部内容,希望文章能够帮你解决c# – 以编程方式在WPF中创建一个网格作为模板所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1260301.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存