Issue
Here's what I have:
var grid = new Grid();
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1) });
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
Is there a way I can combine any of these lines together?
Solution
Not that I know of at present but if it is something you are doing often then you could make it easier via an extension method...
public static class GridExtensions
{
public static Grid Add(this Grid grid, ColumnDefinition columnDefinition)
{
grid.ColumnDefinitions.Add(columnDefinition);
return grid;
}
public static Grid AddColumnDefinition(this Grid grid, GridLength gridLength) =>
grid.Add(new ColumnDefinition {Width = gridLength});
}
Then you can call it like:
grid.AddColumnDefinition(new GridLength(1, GridUnitType.Star))
.AddColumnDefinition(new GridLength(1))
.AddColumnDefinition(new GridLength(1, GridUnitType.Star));
You could even take it a step further and accept the parameters for constructing GridLength
and do that inside your extension method.
I am happy to accept that this is not providing fewer lines but it is less verbose and easier to read.
Answered By - Bijington
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.