Exemplo n.º 1
0
        /// <summary>
        /// 创建单列
        /// </summary>
        /// <param name="column"></param>
        /// <param name="bindingParameter"></param>
        /// <returns></returns>
        public static DataGridTemplateColumn CreateTemplateSingleColumn(ColumnItem column, bool bindingParameter)
        {
            if (column == null)
            {
                throw new ArgumentNullException("column");
            }
            if (String.IsNullOrWhiteSpace(column.BindingName))
            {
                throw new ArgumentNullException("column.BindingName", "末级列的绑定名称不能为 null 或空白字符串。");
            }
            //创建DataGrid的列
            var templateColumn = new DataGridTemplateColumn();

            //设置列的宽度
            templateColumn.Width = new DataGridLength(column.Width);
            //构建模板字符串
            var sbStr = new StringBuilder();

            //根据模板创建标题
            sbStr.AppendLine(CreateDataGridTemplateColumnHeaderContent(String.IsNullOrWhiteSpace(column.ExtendName) ? column.Name : column.ExtendName, -1, -1, -1, -1));
            //动态构建标题样式 DataGridTemplateColumn.HeaderStyle
            #region DataGridTemplateColumn.HeaderStyle
            var headerStyleStr = HeaderStyleString();
            headerStyleStr             = headerStyleStr.Replace("{#content#}", sbStr.ToString());
            templateColumn.HeaderStyle = (Style)XamlReader.Parse(headerStyleStr);
            sbStr.Clear();
            #endregion
            //动态构建绑定DataTemplate DataGridTemplateColumn.CellTemplate
            #region DataGridTemplateColumn.CellTemplate
            //构建绑定模板
            IDataGridDataTemplateFactory templateFactory = ServiceFactory.GetService <IDataGridDataTemplateFactory>();
            IDataGridDataTemplateService templateService = templateFactory.GetService(column.Type);
            sbStr.AppendLine(templateService.CreateCellXaml(column, bindingParameter, null));
            String cellTemplateStr = CellDataTemplateString();
            cellTemplateStr             = cellTemplateStr.Replace("{#content#}", sbStr.ToString());
            templateColumn.CellTemplate = (DataTemplate)XamlReader.Parse(cellTemplateStr);
            #endregion
            return(templateColumn);
        }
Exemplo n.º 2
0
 /// <summary>
 /// 设置单元格绑定
 /// </summary>
 /// <param name="sbStr"></param>
 /// <param name="column"></param>
 /// <param name="bindingParameter"></param>
 /// <param name="index"></param>
 private static void SetCellBinding(StringBuilder sbStr, ColumnItem column, bool bindingParameter, ref int index)
 {
     if (column.Columns.Count > 0)
     {
         foreach (var item in column.Columns)
         {
             SetCellBinding(sbStr, item, bindingParameter, ref index);
         }
     }
     else
     {
         if (index > 0)
         {
             sbStr.AppendLine(String.Format("<Rectangle Fill=\"#FFC9CACA\" VerticalAlignment=\"Stretch\" Grid.Column=\"{0}\"/>", index));
             index++;
         }
         //构建指定类型的项绑定
         IDataGridDataTemplateFactory templateFactory = ServiceFactory.GetService <IDataGridDataTemplateFactory>();
         IDataGridDataTemplateService templateService = templateFactory.GetService(column.Type);
         sbStr.AppendLine(templateService.CreateCellXaml(column, bindingParameter, index));
         index++;
     }
 }