WPF网格 - 绘制自定义网格线

时间:2023-01-24 10:04:30

Say I have a very simple WPF grid (6 rows x 6 columns) defined as follows:

假设我有一个非常简单的WPF网格(6行x 6列),定义如下:

<Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
</Grid.RowDefinitions>

<Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>

I want the following grid lines (one solid line and two dashed lines) drawn as follows (I drew this in Excel so ignore the light Excel grid lines):

我想要如下绘制下面的网格线(一条实线和两条虚线)(我在Excel中绘制了这个,所以忽略了轻的Excel网格线):

WPF网格 - 绘制自定义网格线

How might I go about doing this in XAML?

我怎么能在XAML中这样做呢?

2 个解决方案

#1


14  

You can place Lines at the tops of the required cells, by setting VerticalAlignment="Top", the appropriate Grid.ColumnSpan, and set StrokeDashArray to get the dashed line.

您可以将Lines放置在所需单元格的顶部,方法是设置VerticalAlignment =“Top”,相应的Grid.ColumnSpan,并设置StrokeDashArray以获取虚线。

Edit: The above was just off the top of my head, and I apparently forgot about a few 'features' of WPF.

编辑:以上只是我的头脑,我显然忘记了WPF的一些“功能”。

Here is the sample I got working. I put it in a Grid with 5 rows and columns, star-sized.

这是我工作的样本。我把它放在一个有5行和列的网格中,星号。

<Line Grid.Row="0" Grid.Column="1" Grid.RowSpan="2" Grid.ColumnSpan="2"
    VerticalAlignment="Center" Stroke="Black" StrokeThickness="1"
    X2="{Binding Path=ActualWidth, RelativeSource={RelativeSource Self}}" />
<Line Grid.Row="1" Grid.Column="2" Grid.RowSpan="2" Grid.ColumnSpan="2"
    VerticalAlignment="Center" Stroke="Black" StrokeThickness="2" StrokeDashArray="5,3"
    X2="{Binding Path=ActualWidth, RelativeSource={RelativeSource Self}}" />

Note: If rows will vary in size, that won't work, since it centers the line in the two rows. If they will vary in size, you will need VerticalAlignment="Top", but beware, the top half of the line will be clipped.

注意:如果行的大小不同,那将无效,因为它将行放在两行中心。如果它们的大小不同,则需要VerticalAlignment =“Top”,但要注意,该行的上半部分将被剪裁。

#2


0  

The Grid cells do not have grid lines but the effect is easy to accomplish by simply placing Rectangle(s) with explicit Stroke(s) within cells...

Grid单元格没有网格线,但只需在单元格中放置带有显式Stroke的Rectangle(s)即可轻松实现效果...

EDIT: Didn't notice the dashed lines requirement, for that you can place another grid with one row and desired number of columns within the cell and place Rectangles with Strokes within non-adjecent cells...

编辑:没有注意到虚线要求,因为您可以在单元格中放置另一个网格,其中包含一行和所需数量的列,并在非关系单元格中放置带有笔划的矩形...

#1


14  

You can place Lines at the tops of the required cells, by setting VerticalAlignment="Top", the appropriate Grid.ColumnSpan, and set StrokeDashArray to get the dashed line.

您可以将Lines放置在所需单元格的顶部,方法是设置VerticalAlignment =“Top”,相应的Grid.ColumnSpan,并设置StrokeDashArray以获取虚线。

Edit: The above was just off the top of my head, and I apparently forgot about a few 'features' of WPF.

编辑:以上只是我的头脑,我显然忘记了WPF的一些“功能”。

Here is the sample I got working. I put it in a Grid with 5 rows and columns, star-sized.

这是我工作的样本。我把它放在一个有5行和列的网格中,星号。

<Line Grid.Row="0" Grid.Column="1" Grid.RowSpan="2" Grid.ColumnSpan="2"
    VerticalAlignment="Center" Stroke="Black" StrokeThickness="1"
    X2="{Binding Path=ActualWidth, RelativeSource={RelativeSource Self}}" />
<Line Grid.Row="1" Grid.Column="2" Grid.RowSpan="2" Grid.ColumnSpan="2"
    VerticalAlignment="Center" Stroke="Black" StrokeThickness="2" StrokeDashArray="5,3"
    X2="{Binding Path=ActualWidth, RelativeSource={RelativeSource Self}}" />

Note: If rows will vary in size, that won't work, since it centers the line in the two rows. If they will vary in size, you will need VerticalAlignment="Top", but beware, the top half of the line will be clipped.

注意:如果行的大小不同,那将无效,因为它将行放在两行中心。如果它们的大小不同,则需要VerticalAlignment =“Top”,但要注意,该行的上半部分将被剪裁。

#2


0  

The Grid cells do not have grid lines but the effect is easy to accomplish by simply placing Rectangle(s) with explicit Stroke(s) within cells...

Grid单元格没有网格线,但只需在单元格中放置带有显式Stroke的Rectangle(s)即可轻松实现效果...

EDIT: Didn't notice the dashed lines requirement, for that you can place another grid with one row and desired number of columns within the cell and place Rectangles with Strokes within non-adjecent cells...

编辑:没有注意到虚线要求,因为您可以在单元格中放置另一个网格,其中包含一行和所需数量的列,并在非关系单元格中放置带有笔划的矩形...