question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal: New Grid ColumnDefinition and RowDefinition Constructors

See original GitHub issue

Proposal: New ColumnDefinition and RowDefinition Constructors

Summary

@robloo:

I’ve often wondered why the constructors for ColumnDefinition and RowDefinition don’t have an overloaded constructor that allows passing a GridLength directly. Adding this would potentially simplify a lot of code. I know this doesn’t matter in pure MVVM, but when you have to do code-behind it would be quite helpful.

@anawishnoff:

With the recent changes to the Grid definition syntax in XAML, it seemed only natural to continue this progression towards simpler Grid definitions in code-behind. Currently, the ColumnDefinition and RowDefinition constructors takes no arguments, and in order to create a Grid with one column it takes four lines of code (as can be seen in the example below).

These proposed API changes would allow the ColumnDefinition and RowDefinition constructors to accept two arguments: an integer Width (or Height), and a GridUnitType object (i.e. Auto or Star *). These two parameters will create a GridLength object, which will be assigned to the Width or Height property.

Goal

@anawishnoff:

By defining the height/width and GridUnitType inside of the ColumnDefinition or RowDefinition constructor, you could cut the amount of required lines of code to define a Column or Row in half. This syntax change will make defining rows and columns in the code-behind easier and more consistent with the new way of defining them in XAML.

Example

@robloo:

Before:

var grid = new Grid();
var column = new ColumnDefinition();
column.Width = new GridLength(1.0, GridUnitType.Star);
grid.ColumnDefinitions.Add(column);

After:

(added from @chingucoding comments below)

var grid = new Grid();
grid.ColumnDefinitions.Add(new ColumnDefinition(1.0, GridUnitType.Star)); // Width="1*"
grid.ColumnDefinitions.Add(new ColumnDefinition(500.0)); // Width="500px"

This would be especially helpful when creating multiple rows/columns.

Constructor API

// Constructors with parameters from the GridLength as well
public ColumnDefinition(double pixelWidth) { ... }
public ColumnDefinition(double width, GridUnitType type) { ... }
public RowDefinition(double pixelHeight) { ... }
public RowDefinition(double height, GridUnitType type) { ... }

Rationale

@robloo:

Code-behind simplification. Shorter is better!

@anawishnoff:

  • This new shorter syntax will provide an easier experience in defining one of the most common controls.

  • This change follows a natural progression from the new XAML language feature that will shorten the Grid syntax - defining a Grid in the code-behind and defining a Grid in XAML will be consistent and short experiences.

Scope

Capability Priority
Add overloaded constructor for ColumnDefinition with GridLength width Must
Add overloaded constructor for RowDefinition with GridLength height Must

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:3
  • Comments:26 (24 by maintainers)

github_iconTop GitHub Comments

2reactions
anawishnoffcommented, Dec 16, 2019

Pitched this to our team here, and we decided that the second syntax that involves giving RowDefinition() and ColumnDefinition() the arguments of GridLength works best. Updated the proposal above to reflect the changes! Thanks everyone for your contributions - this will be moved to the spec-level soon 😃

2reactions
anawishnoffcommented, Dec 12, 2019

@robloo:

but it will also expose ColumnDefinition and RowDefinition’s properties in a way that makes the definitions more cohesive and easier to understand.

I’m not sure I understand this. It’s only the constructor parameters that are changing. How would the properties be simplified? Maybe this is just a type-o?

Fixed this - just deleted and replaced that sentence, it didn’t make too much sense/impact reading over it again.

With an object initializer that leaves out the explicit GridLength definition, basically passing GridLength’s arugments into the Column/RowDefinition itself:

I wouldn’t say object initializer here – that’s not actually what @chingucoding was stating here. Instead he was saying we should just add the GridLength constructor parameters directly to the ColumnDefinition/RowDefinition constructors. It can easily be inferred that those represent width or height respectively. Then the ColumnDefinition/RowDefinition would build it’s own GridLength internally and use that for width/height. This would eliminate the need for the app developer to create a GridLength object in this case.

Fixed! I think my words just got a little jumbled, tried to clarify.

Read more comments on GitHub >

github_iconTop Results From Across the Web

WPF: How to dynamically create a grid with x rows and y ...
I would start with an ItemsControl. You can give it a collection of items, and it will render each item however you want,...
Read more >
How do I set a grid column/row size without defining each ...
I propose deriving from Grid and adding these properties to it like ... Add(new RowDefinition()); } } public int NumberOfColumns { get ...
Read more >
AdvancedColumnLayout.RowDefinitions Property
The AdvancedColumnLayout allows you to span cells across multiple columns/rows. To enable the advanced layout, initialize the DataGridView.AdvancedColumnLayout ...
Read more >
ColumnDefinition Class (Windows.UI.Xaml.Controls)
Defines column-specific properties that apply to Grid objects.
Read more >
WPF Grid Column and Row Hiding
How to easily hide columns and rows in a WPF Grid. Background. The RowDefinition and ColumnDefinition are missing an obvious Visibility property ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found