• WPF 后台重写 DataTemplate


        /// <summary>
        /// 配置类
        /// </summary>
        public static class GridControlDeploy
        {
            /// <summary>
            /// 重写 DataTemplate 表头
            /// </summary>
            /// <param name="Widths">GridControl 实际宽度</param>
            /// <param name="StrList">表头名称列表</param>
            /// <returns></returns>
            public static DataTemplate DynamicAddColumn(double Widths, List<string> 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;
            }
        }

     重写表头

  • 相关阅读:
    博客第一篇:博客申请理由
    Cookie基础
    滚动篇————附一个简单单的自定义滚动条
    javascript中对字符串的操作总结
    javascript中创建对象的几种方式
    javascript中event汇总
    ...python scrapy
    Ubuntu 检测到系统出现问题 弹窗 嘿嘿
    万一哪天笔记全没了, 你真正记住的还有多少
    windows10安装mysql5.7.17是这样安装的吗?
  • 原文地址:https://www.cnblogs.com/SeNaiTes/p/10383869.html
Copyright © 2020-2023  润新知