有幾種方法可以嘗試。如果您在Grid.cs看看,看到Brushes.Blue & Brushes.Yellow純色組成你看,當你在下面的源使ShowGridLines =「真」破折號?您可以將它們設置爲不同的顏色(使它們具有相同的顏色,而不必像Brushes.Gray那樣對其進行編輯,或者可以省略短劃線,繪製一條線,甚至可以使用自定義畫筆資源,如梯度。
/// <summary>
/// Helper to render grid lines.
/// </summary>
internal class GridLinesRenderer : DrawingVisual
{
/// <summary>
/// Static initialization
/// </summary>
static GridLinesRenderer()
{
s_oddDashPen = new Pen(Brushes.Blue, c_penWidth);
DoubleCollection oddDashArray = new DoubleCollection();
oddDashArray.Add(c_dashLength);
oddDashArray.Add(c_dashLength);
s_oddDashPen.DashStyle = new DashStyle(oddDashArray, 0);
s_oddDashPen.DashCap = PenLineCap.Flat;
s_oddDashPen.Freeze();
s_evenDashPen = new Pen(Brushes.Yellow, c_penWidth);
DoubleCollection evenDashArray = new DoubleCollection();
evenDashArray.Add(c_dashLength);
evenDashArray.Add(c_dashLength);
s_evenDashPen.DashStyle = new DashStyle(evenDashArray, c_dashLength);
s_evenDashPen.DashCap = PenLineCap.Flat;
s_evenDashPen.Freeze();
}
或者有一招您可以在XAML顯示做(因爲我已經打了一個例子一起在別的地方過去後),您採取邊境控制一組BorderBrush &了borderThickness和跨度像這個例子一樣,在單元格的增量邊框&列;
<Border Height="300" Width="300" Background="White" BorderThickness="1" BorderBrush="Gray">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="30"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<!-- Horizontal Accent Lines -->
<Border Grid.ColumnSpan="10" BorderThickness="0,1" BorderBrush="Gray"/>
<Border Grid.Row="2" Grid.ColumnSpan="10" BorderThickness="0,1" BorderBrush="Gray"/>
<Border Grid.Row="4" Grid.ColumnSpan="10" BorderThickness="0,1" BorderBrush="Gray"/>
<Border Grid.Row="6" Grid.ColumnSpan="10" BorderThickness="0,1" BorderBrush="Gray"/>
<Border Grid.Row="8" Grid.ColumnSpan="10" BorderThickness="0,1" BorderBrush="Gray"/>
<!-- Vertical Accent Lines -->
<Border Grid.Column="1" Grid.RowSpan="10" BorderThickness="1,0" BorderBrush="Gray"/>
<Border Grid.Column="3" Grid.RowSpan="10" BorderThickness="1,0" BorderBrush="Gray"/>
<Border Grid.Column="5" Grid.RowSpan="10" BorderThickness="1,0" BorderBrush="Gray"/>
<Border Grid.Column="7" Grid.RowSpan="10" BorderThickness="1,0" BorderBrush="Gray"/>
<Border Grid.Column="9" Grid.RowSpan="10" BorderThickness="1,0" BorderBrush="Gray"/>
<!-- Content Elements -->
<TextBlock Text="1" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<TextBlock Text="2" Grid.Column="5" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<TextBlock Text="3" Grid.Row="2" Grid.Column="9" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<TextBlock Text="4" Grid.Row="4" Grid.Column="3" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<TextBlock Text="5" Grid.Row="8" Grid.Column="7" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Border>
或使用XAML for DataGrid;
<Window.Resources>
<SolidColorBrush x:Key="RedGridLine" Color="#FFFF4444" />
<SolidColorBrush x:Key="BlueGridLine" Color="#554444FF"/>
</Window.Resources>
<my:DataGrid VerticalGridLinesBrush="{StaticResource RedGridLine}"
HorizontalGridLinesBrush="{StaticResource BlueGridLine}" >
希望這會有所幫助,祝你好運!
我不確定我是否理解你的問題,但我認爲網格控件的ShowGridLines屬性不是解決方案,對吧? – michele 2012-02-03 10:01:22
ShowGridLines上的MSDN:啓用網格線會在網格中的所有元素周圍創建虛線。只有虛線可用,因爲此屬性旨在作爲調試佈局問題的設計工具,並不適用於生產質量代碼。如果你想在一個Grid中使用線條,可以使Grid內的元素具有邊框的風格。「你可能會更好用[DataGrid](http://msdn.microsoft.com/zh-cn/library/system .windows.controls.datagrid(v = VS.95)的.aspx)? – Clemens 2012-02-03 10:04:57