private void DrawGridLines() { if (Target == null) { return; } var drawingContext = _gridLinesVisual.RenderOpen(); var itemCount = Target.Items.Count; if (itemCount == 0) { drawingContext.Close(); return; } var gridView = Target.View as GridView; // 获取drawingContext var startPoint = new Point(0, 0); var hLineY = 0.0; // 为了对齐到像素的计算参数,否则就会看到有些线是模糊的 var dpiFactor = this.GetDpiFactor(); var pen = new Pen(this.GridLineBrush, 1 * dpiFactor); var halfPenWidth = pen.Thickness / 2; var guidelines = new GuidelineSet(); // 画横线 var lvRender = Target.RenderSize; var offsetS = Target.TranslatePoint(startPoint, this); var hLineX1 = offsetS.X + Target.BorderThickness.Left; var hLineX2 = offsetS.X + _viewWidth; bool firstLine = true; for (int i = 0; i < itemCount; i++) { var item = Target.ItemContainerGenerator.ContainerFromIndex(i) as ListViewItem; if (item != null) { var renderSize = item.RenderSize; var offset = item.TranslatePoint(startPoint, this); if (offset.Y < offsetS.Y) { continue; } hLineY = offset.Y; if (firstLine) { firstLine = false; // 加入参考线,对齐到像素 guidelines.GuidelinesY.Add(hLineY + halfPenWidth); drawingContext.PushGuidelineSet(guidelines); drawingContext.DrawLine(pen, new Point(hLineX1, hLineY), new Point(hLineX2, hLineY)); drawingContext.Pop(); } hLineY += renderSize.Height; if (hLineY > offsetS.Y + Target.ActualHeight) { continue; } // 加入参考线,对齐到像素 guidelines.GuidelinesY.Add(hLineY + halfPenWidth); drawingContext.PushGuidelineSet(guidelines); drawingContext.DrawLine(pen, new Point(hLineX1, hLineY), new Point(hLineX2, hLineY)); drawingContext.Pop(); } } // 画竖线 if (null == gridView || null == _headerRowPresenter || !_HorizontalLine) { drawingContext.Close(); return; } var columns = gridView.Columns; var headerOffset = _headerRowPresenter.TranslatePoint(startPoint, this); var headerSize = _headerRowPresenter.RenderSize; var vLineX = headerOffset.X; var vLineY1 = headerOffset.Y + headerSize.Height; if (hLineY > offsetS.Y + Target.ActualHeight) { hLineY = offsetS.Y + Target.ActualHeight; } firstLine = _leftHorizontalLine; foreach (var column in columns) { var columnWidth = column.GetColumnWidth(); if (firstLine) { firstLine = false; if (offsetS.X >= headerOffset.X + _viewWidth) { continue; } // 加入参考线,对齐到像素 guidelines.GuidelinesX.Add(offsetS.X + halfPenWidth); drawingContext.PushGuidelineSet(guidelines); drawingContext.DrawLine(pen, new Point(offsetS.X, vLineY1), new Point(offsetS.X, hLineY)); drawingContext.Pop(); } vLineX += columnWidth; if (vLineX >= headerOffset.X + _viewWidth) { continue; } // 加入参考线,对齐到像素 guidelines.GuidelinesX.Add(vLineX + halfPenWidth); drawingContext.PushGuidelineSet(guidelines); drawingContext.DrawLine(pen, new Point(vLineX, vLineY1), new Point(vLineX, hLineY)); drawingContext.Pop(); } drawingContext.Close(); }
private void DrawGridLines() { if (Target == null) { return; } if (_headerRowPresenter == null) { return; } var itemCount = Target.Items.Count; if (itemCount == 0) { return; } var gridView = Target.View as GridView; if (gridView == null) { return; } // 获取drawingContext var drawingContext = _gridLinesVisual.RenderOpen(); var startPoint = new Point(0, 0); // 为了对齐到像素的计算参数,否则就会看到有些线是模糊的 var dpiFactor = this.GetDpiFactor(); var pen = new Pen(this.GridLineBrush, 1 * dpiFactor); var halfPenWidth = pen.Thickness / 2; var guidelines = new GuidelineSet(); // 计算表头的偏移量和大小 var headerOffset = _headerRowPresenter.TranslatePoint(startPoint, this); var headerSize = _headerRowPresenter.RenderSize; var headerBottomY = headerOffset.Y + headerSize.Height; // 计算ScrollViewer的可视区域大小 var item0 = _target.ItemContainerGenerator.ContainerFromIndex(0); if (item0 == null) { return; } var scrollViewer = item0.GetAncestor <ScrollViewer>(); if (scrollViewer == null) { return; } var contentElement = scrollViewer.Content as UIElement; var maxLineX = scrollViewer.ViewportWidth; var maxLineY = headerBottomY + contentElement.RenderSize.Height; var vLineY = 0.0; // 画横线 for (int i = 0; i < itemCount; i++) { var item = Target.ItemContainerGenerator.ContainerFromIndex(i) as ListViewItem; if (item != null) { var renderSize = item.RenderSize; var offset = item.TranslatePoint(startPoint, this); var hLineX1 = offset.X; var hLineX2 = offset.X + renderSize.Width; var hLineY = offset.Y + renderSize.Height; vLineY = hLineY; // 小于视图起始位置的不绘制 if (hLineY <= headerBottomY) { continue; } // 大于视图结束位置之后的不绘制 if (hLineY > maxLineY) { break; } // 如果大于横向宽度,取横向宽度 if (hLineX2 > maxLineX) { hLineX2 = maxLineX; } // 加入参考线,对齐到像素 guidelines.GuidelinesY.Add(hLineY + halfPenWidth); drawingContext.PushGuidelineSet(guidelines); drawingContext.DrawLine(pen, new Point(hLineX1, hLineY), new Point(hLineX2, hLineY)); drawingContext.Pop(); } } // 画竖线 var columns = gridView.Columns; var vLineX = headerOffset.X; if (vLineY > maxLineY) { vLineY = maxLineY; } foreach (var column in columns) { var columnWidth = column.GetColumnWidth(); vLineX += columnWidth; if (vLineX > maxLineX) { break; } // 加入参考线,对齐到像素 guidelines.GuidelinesX.Add(vLineX + halfPenWidth); drawingContext.PushGuidelineSet(guidelines); drawingContext.DrawLine(pen, new Point(vLineX, headerBottomY), new Point(vLineX, vLineY)); drawingContext.Pop(); } drawingContext.Close(); }