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

前端之家收集整理的这篇文章主要介绍了c# – 以编程方式在WPF中创建一个网格作为模板前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想用编程方式创建一个基本的用户控件.
在这种风格我想添加一个网格(没有问题),但我不能添加列定义到这个网格.

我的示例代码

  1. ControlTemplate templ = new ControlTemplate();
  2. FrameworkElementFactory mainPanel = new FrameworkElementFactory(typeof(DockPanel));
  3. mainPanel.SetValue(DockPanel.LastChildFillProperty,true);
  4.  
  5. FrameworkElementFactory headerPanel = new FrameworkElementFactory(typeof(StackPanel));
  6. headerPanel.SetValue(StackPanel.OrientationProperty,Orientation.Horizontal);
  7. headerPanel.SetValue(DockPanel.DockProperty,Dock.Top);
  8. mainPanel.AppendChild(headerPanel);
  9.  
  10. FrameworkElementFactory headerImg = new FrameworkElementFactory(typeof(Image));
  11. headerImg.SetValue(Image.MarginProperty,new Thickness(5));
  12. headerImg.SetValue(Image.HeightProperty,32d);
  13. headerImg.SetBinding(Image.SourceProperty,new Binding("ElementImage") { RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent) });
  14. headerPanel.AppendChild(headerImg);
  15.  
  16. FrameworkElementFactory headerTitle = new FrameworkElementFactory(typeof(TextBlock));
  17. headerTitle.SetValue(TextBlock.FontSizeProperty,16d);
  18. headerTitle.SetValue(TextBlock.VerticalAlignmentProperty,VerticalAlignment.Center);
  19. headerTitle.SetBinding(TextBlock.TextProperty,new Binding("Title") { RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent) });
  20. headerPanel.AppendChild(headerTitle);
  21.  
  22. FrameworkElementFactory mainGrid = new FrameworkElementFactory(typeof(Grid));
  23. FrameworkElementFactory c1 = new FrameworkElementFactory(typeof(ColumnDefinition));
  24. c1.SetValue(ColumnDefinition.WidthProperty,new GridLength(1,GridUnitType.Star));
  25. FrameworkElementFactory c2 = new FrameworkElementFactory(typeof(ColumnDefinition));
  26. c2.SetValue(ColumnDefinition.WidthProperty,GridUnitType.Auto));
  27. FrameworkElementFactory c3 = new FrameworkElementFactory(typeof(ColumnDefinition));
  28. c3.SetValue(ColumnDefinition.WidthProperty,new GridLength(3,GridUnitType.Star));
  29. FrameworkElementFactory colDefinitions = new FrameworkElementFactory(typeof(ColumnDefinitionCollection));
  30. colDefinitions.AppendChild(c1);
  31. colDefinitions.AppendChild(c2);
  32. colDefinitions.AppendChild(c3);
  33. mainGrid.AppendChild(colDefinitions);
  34.  
  35. mainPanel.AppendChild(mainGrid);
  36.  
  37. FrameworkElementFactory content = new FrameworkElementFactory(typeof(ContentPresenter));
  38. content.SetBinding(ContentPresenter.ContentProperty,new Binding() { RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent),Path = new PropertyPath("Content") });
  39. mainGrid.AppendChild(content);
  40.  
  41. templ.VisualTree = mainPanel;
  42. Style mainStyle = new Style();
  43. mainStyle.Setters.Add(new Setter(UserControl.TemplateProperty,templ));
  44. this.Style = mainStyle;

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

谁能帮我?

解决方法

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

猜你在找的C#相关文章