/// <summary> /// 设置标题内容 /// </summary> /// <param name="sbStr"></param> /// <param name="column">当前列</param> /// <param name="totalcolumnCount">总列数</param> /// <param name="colIndex">内容索引列</param> private static void SetContentPresenter(StringBuilder sbStr, ColumnItem column, int totalcolumnCount, ref int colIndex) { //计算出当前列所占的Grid的ColumnDefinition数目 int columnOffset = column.LastLevelColumnCount() * 2 - 1; //计算当前列在整个标题中的标题行深度 int LevelDepth = column.Parent.ChildLevelDepth(); //用于控制绘制标题内容下面显示下线,以及当前显示标题的Grid.RowSpan int rowOffset; //一般情况默认为1 if (column.Columns.Count == 0) // { rowOffset = LevelDepth * 2 - 1; } else { rowOffset = 1; // } //计算出当前标题在第几个RowDefinition画内容下边线 int lineRow = (column.Level * 2 + 1) + (rowOffset - 1); //计算出当前标题在第几个ColumnDefinition画内容右边线 int lineColumn = (colIndex + 1) + (columnOffset - 1); //画标题,并设置当前标题在Grid中的定位 sbStr.AppendLine( CreateDataGridTemplateColumnHeaderContent( String.IsNullOrWhiteSpace(column.ExtendName) ? column.Name : column.ExtendName, //标题显示内容 column.Level * 2, //所属行,标题内容显示的Grid.Row rowOffset, //标题内容显示的Grid.RowSpan colIndex, //标题内容显示的Grid.Column列 columnOffset //标题内容显示的Grid.ColumnSpan )); //存在子级,时添加下线 if (column.Columns.Count > 0) { sbStr.AppendLine(String.Format("<Rectangle Fill=\"#FFC9CACA\" VerticalAlignment=\"Stretch\" Height=\"1\" Grid.Row=\"{0}\" Grid.Column=\"{1}\" Grid.RowSpan=\"{2}\" Grid.ColumnSpan=\"{3}\" />", lineRow, colIndex, 1, columnOffset)); } //标题右侧下线 if (lineColumn < (totalcolumnCount * 2 - 1)) { sbStr.AppendLine(String.Format("<Rectangle Fill=\"#FFC9CACA\" VerticalAlignment=\"Stretch\" Width=\"1\" Visibility=\"Visible\" Grid.Row=\"{0}\" Grid.Column=\"{1}\" Grid.RowSpan=\"{2}\" Grid.ColumnSpan=\"{3}\" />", column.Level * 2, lineColumn, rowOffset, 1)); } //存在子级,先从子级起画 if (column.Columns.Count > 0) { foreach (var item in column.Columns) { SetContentPresenter(sbStr, item, totalcolumnCount, ref colIndex); } } else { colIndex += 2; //含分隔线 } }
/// <summary> /// 创建组列 /// </summary> /// <param name="column">列</param> /// <param name="bindingParameter">是否是参数</param> /// <returns></returns> public static DataGridTemplateColumn CreateTemplateGroupColumn(ColumnItem column, bool bindingParameter) { var templateColumn = new DataGridTemplateColumn(); //当前列包含的子级深度 int LevelDepth = column.ChildLevelDepth(); //获取当前列子级的总列数 int totalcolumnCount = column.LastLevelColumnCount(); // //设置当前列的宽度 templateColumn.Width = new DataGridLength(column.TotalGroupWidth() + LevelDepth); //构建HeaderStyle以及CellTemplate模板 //动态构建标题样式 DataGridTemplateColumn.HeaderStyle #region 构建多行标题 var sbHeaderStr = new StringBuilder(); sbHeaderStr.AppendLine("<Grid.RowDefinitions>"); for (int i = 0; i <= LevelDepth; i++) { if (i > 0) { sbHeaderStr.AppendLine("<RowDefinition Height=\"1\" />"); //构建分割线, } if (i < LevelDepth) { sbHeaderStr.AppendLine("<RowDefinition Height=\"25\" />"); //内容区域 } else { sbHeaderStr.AppendLine("<RowDefinition Height=\"*\" MinHeight=\"25\"/>"); //内容区域 } } sbHeaderStr.AppendLine("</Grid.RowDefinitions>"); sbHeaderStr.AppendLine("<Grid.ColumnDefinitions>"); int index = 0; foreach (var item in column.Columns) { SetColumnDefinition(sbHeaderStr, item, ref index); } sbHeaderStr.AppendLine("</Grid.ColumnDefinitions>"); int columnOffset = totalcolumnCount * 2 - 1; sbHeaderStr.AppendLine(CreateDataGridTemplateColumnHeaderContent(String.IsNullOrWhiteSpace(column.ExtendName) ? column.Name : column.ExtendName, -1, -1, -1, columnOffset)); sbHeaderStr.AppendLine(String.Format("<Rectangle Fill=\"#FFC9CACA\" VerticalAlignment=\"Stretch\" Height=\"1\" Grid.Row=\"1\" Grid.ColumnSpan=\"{0}\" />", columnOffset)); index = 0; foreach (var item in column.Columns) { SetContentPresenter(sbHeaderStr, item, totalcolumnCount, ref index); } var headerStyleStr = HeaderStyleString(); headerStyleStr = headerStyleStr.Replace("{#content#}", sbHeaderStr.ToString()); templateColumn.HeaderStyle = (Style)XamlReader.Parse(headerStyleStr); #endregion //动态构建绑定DataTemplate DataGridTemplateColumn.CellTemplate #region 构建多行标题数据绑定模板 var sbCellTempStr = new StringBuilder(); sbCellTempStr.AppendLine("<Grid.ColumnDefinitions>"); index = 0; foreach (var item in column.Columns) { SetCellColumnDefinition(sbCellTempStr, item, ref index); } sbCellTempStr.AppendLine("</Grid.ColumnDefinitions>"); index = 0; foreach (var item in column.Columns) { SetCellBinding(sbCellTempStr, item, bindingParameter, ref index); } var cellTemplateStr = CellDataTemplateString(); cellTemplateStr = cellTemplateStr.Replace("{#content#}", sbCellTempStr.ToString()); templateColumn.CellTemplate = (DataTemplate)XamlReader.Parse(cellTemplateStr); #endregion return(templateColumn); }