Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use Border over Grid?

The documentation for the Grid class states:

Starting in Windows 10, Grid defines new border properties that let you draw a border around the Grid without using an additional Border element. The new properties are Grid.BorderBrush, Grid.BorderThickness, Grid.CornerRadius, and Grid.Padding.

Does this make the Border class redundant? Why would I ever want to use a Border if I can do the same thing with a Grid and lay out children in a grid fashion if I so choose?


EDIT

I generally agree with Bart's answer, but there's a few things I would like to talk about.

Firstly, other layout controls (like StackPanel) do indeed have Border-like properties such as BorderBrush, BorderThickness and CornerRadius, so Grid isn't the only layout control which gets these new perks. But they are not attached properties provided by the Border class (or have anything to do with the Border class at all).

Secondly, I, too, thought that it would be a overkill to use complex layout control like Grid if I only wanted to add a border to a simple control like an image. But surely these layout controls would be optimized such that any impact to layout rendering speed would be negligible if they only contained a single child without any special layout constraints. So using a Grid instead of a Border shouldn't affect performance when it only has a single child.

Lastly, I decided to find out just how Grid achieves the border appearance. I created a simple Page containing a Grid with a border and a single Rectangle child, like this:

<Grid Background="Red" BorderBrush="Blue" BorderThickness="10">
    <Rectangle Width="50" Height="50" HorizontalAlignment="Left" VerticalAlignment="Top" Fill="Yellow"/>
</Grid>

with the following rendering:

Screenshot

Upon inspecting the live visual tree, the Grid class does not add any additional elements to the visual tree to make the border appearance:

Screenshot

I suspect whatever mechanism it uses to create the border appearance would be the same as what the Border control does (maybe it creates an additional Visual in the composition layer).

So I'm not really convinced that the "simpler" Border control would be better to use because it looks like the Grid control is already quite performant. I can't see any evidence for the Grid control adding any extra overhead to the visual tree (when compared to what a Border would do to achieve the same effect).

So far the only reason I can think of why I should use Border instead of Grid is to express meaning. It's obvious what the Border control does, adds a border around a single element. Grid is a layout control used to lay out child elements within a grid (which, as it so happens, has the ability to add a border around its children too as an added bonus).

like image 442
Decade Moon Avatar asked Sep 24 '16 10:09

Decade Moon


People also ask

When should I use grid CSS?

Grids are useful when you want to apply some space between rows, columns, or both. Although Flex does have some gap properties it has lower support at just 75% . So Grid is a preferable choice there.

When should I use grid and flex?

CSS Grid and Flexbox are layout models that share similarities and can even be used together. The main difference is that you can use CSS Grid to create two-dimensional layouts. In contrast, you can only use Flexbox to create one-dimensional layouts.

What is grid gap used for?

grid-column-gap: It sets the size of the gap between the columns in a grid layout.

Should I use CSS Grid Layout?

CSS grid is better when:We can define the gap between our rows or columns very easily, without having to use the margin property, which can cause some side effects especially if we're working with many breakpoints.

How to skip the border around the table using CSS?

Skip the border around the table by leaving out table from the css selector: With the border-style property, you can set the appereance of the border. With the border-color property, you can set the color of the border.

How do I add a border to a table element?

To add a border, use the CSS border property on table, th, and td elements: To avoid having double borders like in the example above, set the CSS border-collapse property to collapse. If you set a background color of each cell, and give the border a white color (the same as the document background), you get the impression of an invisible border:

How do I make a border not double border in CSS?

To avoid having double borders like in the example above, set the CSS border-collapse property to collapse. If you set a background color of each cell, and give the border a white color (the same as the document background), you get the impression of an invisible border: With the border-radius property, the borders get rounded corners:

Why should you use CSS grid?

But for a better CSS approach, to have a more concise, well-written, and maintainable application in the long-term, to create and fit your layout perfectly, the ideal method is to use CSS grid. You have a complex design to implement: In some use cases, we have complex designs to implement, and that’s when the magic of CSS grid shows itself.


1 Answers

Your first question designing your UI should be do I need a Grid or another layout control (StackPanel, RelativePanel, Canvas, ...) based on the specific behavior of each control (see MSDN). Note that next to Grid, also StackPanel and RelativePanel have Border attached properties. These attached properties are bonus for you to not add another 'redundant' control in your XAML tree to wrap your layout control in.

Why is the Border control NOT obsolete? Let's say you want a border around an image (which is inside any of the above mentioned layout controls together with other items, so you can't reuse the layout control's border). Now you have 2 options:

  • wrap the image in a Border control.
  • wrap the image in a Grid (or other layout control) and abuse the attached properties.

My wording should be clear which one is best: pick the Border control. A rule of thumb is: keep your visual tree as small as possible. Opt for the simplest control doing the task. Placing an image in a Grid just for having the border around it adds to much extra overhead in your visual tree.

like image 180
Bart Avatar answered Oct 01 '22 22:10

Bart