WPF Grid - drawing custom grid lines

Let's say I have a very simple WPF grid (6 rows x 6 columns), which is defined as follows:

<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) to be drawn as follows (I drew this in Excel to ignore the light lines of the Excel grid):

enter image description here

How can I do this in XAML?

+6
source share
2 answers

You can put Line at the top of the required cells by setting VerticalAlignment="Top" corresponding to Grid.ColumnSpan and setting StrokeDashArray to get a dashed line.

Edit: The above was just out of my head, and I obviously forgot about a few "features" of WPF.

Here is the sample I received. I put it in a grid with 5 rows and columns the size of a star.

 <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 the lines are different in size, this will not work, because it centers the line in two lines. If they vary in size, you will need VerticalAlignment="Top" , but be careful, the top half of the line will be cut off.

+14
source

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

EDIT: did not notice the dashed line requirements, for which you can put another grid with one row and the desired number of columns inside the cell and put the Rectangles with strokes in the non-address cells ...

0
source

Source: https://habr.com/ru/post/919276/


All Articles