////// 配置类 /// public static class GridControlDeploy { ////// 重写 DataTemplate 表头 /// /// GridControl 实际宽度 /// 表头名称列表 ///public static DataTemplate DynamicAddColumn(double Widths, List StrList) { //去掉两头宽度 Widths = Widths - 35; var gd = new FrameworkElementFactory(typeof(StackPanel)); gd.SetValue(StackPanel.BackgroundProperty, new SolidColorBrush((Color)ColorConverter.ConvertFromString("#D1E8FF"))); gd.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal); gd.SetValue(StackPanel.HeightProperty, 30.0); var Bor1 = new FrameworkElementFactory(typeof(Border)); Bor1.SetValue(Border.WidthProperty, 16.0); Bor1.SetValue(Border.BackgroundProperty, new SolidColorBrush((Color)Colors.White)); Bor1.SetValue(Border.BorderThicknessProperty, new Thickness(0, 0, 1, 1)); Bor1.SetValue(Border.BorderBrushProperty, new SolidColorBrush((Color)ColorConverter.ConvertFromString("#D6D6D6"))); gd.AppendChild(Bor1); foreach (var item in StrList) { var Bor3 = new FrameworkElementFactory(typeof(Border)); Bor3.SetValue(Border.BorderThicknessProperty, new Thickness(0, 0, 1, 1)); Bor3.SetValue(Border.BorderBrushProperty, new SolidColorBrush((Color)ColorConverter.ConvertFromString("#D6D6D6"))); Bor3.SetValue(Border.WidthProperty, Widths / StrList.Count()); var textblockFEF = new FrameworkElementFactory(typeof(TextBlock)); textblockFEF.SetValue(TextBlock.TextProperty, item); textblockFEF.SetValue(TextBlock.ToolTipProperty, item); textblockFEF.SetValue(TextBlock.HorizontalAlignmentProperty, HorizontalAlignment.Center); textblockFEF.SetValue(TextBlock.VerticalAlignmentProperty, VerticalAlignment.Center); textblockFEF.SetValue(TextBlock.TextWrappingProperty, TextWrapping.Wrap); Bor3.AppendChild(textblockFEF); gd.AppendChild(Bor3); } var Bor2 = new FrameworkElementFactory(typeof(Border)); Bor2.SetValue(Border.WidthProperty, 19.0); Bor2.SetValue(Border.BackgroundProperty, new SolidColorBrush((Color)Colors.White)); Bor2.SetValue(Border.BorderThicknessProperty, new Thickness(0, 0, 1, 1)); Bor2.SetValue(Border.BorderBrushProperty, new SolidColorBrush((Color)ColorConverter.ConvertFromString("#D6D6D6"))); gd.AppendChild(Bor2); var dataTemplate = new DataTemplate { VisualTree = gd }; return dataTemplate; } }
重写表头