Proposal: New Grid ColumnDefinition and RowDefinition Constructors
See original GitHub issueProposal: New ColumnDefinition and RowDefinition Constructors
Summary
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.
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
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
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
Code-behind simplification. Shorter is better!
-
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:
- Created 4 years ago
- Reactions:3
- Comments:26 (24 by maintainers)
Pitched this to our team here, and we decided that the second syntax that involves giving
RowDefinition()
andColumnDefinition()
the arguments ofGridLength
works best. Updated the proposal above to reflect the changes! Thanks everyone for your contributions - this will be moved to the spec-level soon 😃@robloo:
Fixed this - just deleted and replaced that sentence, it didn’t make too much sense/impact reading over it again.
Fixed! I think my words just got a little jumbled, tried to clarify.