[Enhancement] Grid definitions in controls #11389
Replies: 14 comments 37 replies
-
or like this:
|
Beta Was this translation helpful? Give feedback.
-
you know that you can already express Col and RowDefs in a more elegant way than what you're using, right ? |
Beta Was this translation helpful? Give feedback.
-
Yes, I know that. I am proposing compacting of attached properties of Grid, like Grid.Column, Grid.Row, Grid.Colspan and Grid.Rowspan to Grid.Cell or Grid.At |
Beta Was this translation helpful? Give feedback.
-
Adding this related discussion about adding names microsoft/microsoft-ui-xaml#2094 <Grid ColumnDefinitions="left:100, main:*, right:100"
RowDefinitions="header:60, body:*, footer:100">
<Label Grid.Cell="left, header" Text="ABC" />
<Label Grid.Cell="left, header, 2" Text="ABC" />
<Label Grid.Cell="main, body" Text="ABC" />
</Grid> |
Beta Was this translation helpful? Give feedback.
-
I would vote for the cell syntax and think the named would be awesome as well! |
Beta Was this translation helpful? Give feedback.
-
And what happens if both cell and grid/column/spans are set? This creates a really weird object model with things fighting each other and mysterious precedence |
Beta Was this translation helpful? Give feedback.
-
Since in the end, it's all setting the row, column and spans anyway I think it's the user's choice if they mix them, between tags, though I wouldn't myself. I would think you should get a compile-time error if you mixed them on the same tag. You can either do the classic attributes, cell, or name...but not a combo of multiple. |
Beta Was this translation helpful? Give feedback.
-
just like @dotMorten , I'm torn on this one. I like the simplicity of it, but I hate having multiple (non-deprecated) ways of setting the same property |
Beta Was this translation helpful? Give feedback.
-
Why not instead create a new grid type where you don't need to set row/col at all, but the order means the cell they go to (and the option to set the span if you need). That's really what we mostly want to do anyway. |
Beta Was this translation helpful? Give feedback.
-
If anyone is interested, here is the code which adds an attached property - using System;
using Xamarin.Forms;
namespace XamarinPlayground.CustomControls
{
/// <summary>
/// Custom Grid control having Cell as attached property for
/// compact definition of Column, Row, Colspan and Rowspan
/// for any given child controls.
/// </summary>
public class Grid : Xamarin.Forms.Grid
{
/// <summary>
/// Define Column, Row, Colspan and/or Rowspan.
/// </summary>
public static readonly BindableProperty CellProperty =
BindableProperty.CreateAttached("Cell", typeof(string), typeof(Grid), null,
propertyChanged: (BindableObject bindable, object oldValue, object newValue) =>
{
if (!(bindable is VisualElement ve))
return;
try
{
if (string.IsNullOrEmpty(newValue.ToString()))
return;
var values = newValue.ToString().Split(',');
if (values.Length >= 1)
SetColumn(ve, Convert.ToInt32(values[0].Trim()));
else
return;
if (values.Length >= 2)
SetRow(ve, Convert.ToInt32(values[1].Trim()));
else
return;
if (values.Length >= 3)
SetColumnSpan(ve, Convert.ToInt32(values[2].Trim()));
else
return;
if (values.Length >= 4)
SetRowSpan(ve, Convert.ToInt32(values[3].Trim()));
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
});
public static string GetCell(BindableObject view) => (string)view.GetValue(CellProperty);
public static void SetCell(BindableObject view, string value) => view.SetValue(CellProperty, value);
}
public static class GridMarkup
{
public static string Cell<TView>(this TView view, string definitions) where TView : VisualElement
{
if (!string.IsNullOrEmpty(definitions))
Grid.SetCell(view, definitions);
return Grid.GetCell(view);
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Could the same feature mentioned in this issue be used to create a compiler error if Cell was added and both Cell and Row, Column, Spans where set? |
Beta Was this translation helpful? Give feedback.
-
What about SharedSizeGroup for columns and rows ? |
Beta Was this translation helpful? Give feedback.
-
I put up a spec for a grid that implies the row and column of its children from the order they are defined in XAML or added in code to the community toolkit. Check it out and leave any thoughts or comments you have. Thanks! |
Beta Was this translation helpful? Give feedback.
-
There is a PR open to add Implied Order Grid Behavior to the Xamarin Community Toolkit. Hopefully will ship with V1. |
Beta Was this translation helpful? Give feedback.
-
Summary
Simplify the grid definitions in controls
API Changes / Additions
eg:
Existing
Proposed
Replacing Column, Row, Colspan and Rowspan with Cell would make the code elegant and compact within grids. This will also help in changing the position more swiftly with less code.
Intended Use Case
Grid is used almost in every page/view in Xamarin.Forms and the controls used within the grid gets too complex just with the addition of column, row, colspan and rowspans. The proposed change is quite similar to what the recent definition compacting has been done by the team and this will extend to the children within the grid and make the code far more simple and compact.
Beta Was this translation helpful? Give feedback.
All reactions