【问题标题】:How to highlight the border lines of a Grid control如何突出显示 Grid 控件的边框线
【发布时间】:2012-02-04 09:30:48
【问题描述】:

我编写了一些代码来将 100 x 100 的单元格添加到网格中。问题是我想突出显示分割网格行/列的线条。

我应该使用什么属性,或者我应该怎么做?

下面是创建网格单元的代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        for (int i = 0; i < 100 ; i++)

            layoutGrid.RowDefinitions.Add( new RowDefinition {  } );
        for (int i = 0; i < 100; i++)
            layoutGrid.ColumnDefinitions.Add(new ColumnDefinition { });

    }
}

【问题讨论】:

  • 我不确定我是否正确理解了您的问题,但我认为网格控件的 ShowGridLines 属性不是解决方案,对吧?
  • ShowGridLines 上的 MSDN:Enabling grid lines creates dotted lines around all the elements within a Grid. Only dotted lines are available because this property is intended as a design tool to debug layout problems and is not intended for use in production quality code. If you want lines inside a Grid, style the elements within the Grid to have borders. 使用DataGrid 不是更好吗?

标签: wpf wpf-controls


【解决方案1】:

您可以尝试几种方法。如果您查看 Grid.cs,请查看构成破折号的 Brushes.BlueBrushes.Yellow 纯色您在下面的源代码中启用 ShowGridLines="True" 吗?您可以将它们设置为不同的颜色(使它们都具有相同的颜色,而不必像 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>

或者为 DataGrid 使用 XAML;

   <Window.Resources>
       <SolidColorBrush x:Key="RedGridLine" Color="#FFFF4444" />
       <SolidColorBrush x:Key="BlueGridLine" Color="#554444FF"/>
    </Window.Resources>

    <my:DataGrid VerticalGridLinesBrush="{StaticResource RedGridLine}"
            HorizontalGridLinesBrush="{StaticResource BlueGridLine}" >

希望这会有所帮助,祝你好运!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-20
    • 2014-11-02
    • 1970-01-01
    • 2011-02-01
    • 2022-01-11
    • 1970-01-01
    • 2018-06-16
    • 2014-02-09
    相关资源
    最近更新 更多