I'm trying to insert new object into existing Grid at row 0 (shifting the remaining rows one lower). Is there a way to do it? Similarly to Log, last item goes in first position. Note that I cannot use ListView, I already have one in the content. Also, I'd prefer to use Grid as I can structure it better for presentation wise etc. Finished grid structure:
> <Grid.RowDefinitions>
> <RowDefinition Height="*"/> </Grid.RowDefinitions>
> <Grid.ColumnDefinitions>
> <ColumnDefinition/>
> <ColumnDefinition/>
> <ColumnDefinition/>
</Grid.ColumnDefinitions>
> (existing Labels)
> <Label Text="1" Grid.Column="0" Grid.Row="0"/>
<Label Text="2" Grid.Column="0" Grid.Row="0"/>
> <Label Text="3", Grid.Column="0", Grid.Row="0"/>
> </>
I'm generating the grid programmatically, to fill out the above structure (iterate column / rows nr), then attempt to insert top row with Child:
MyGrid.RowDefinitions.Insert(0,newDefinition); // Insert new row
> MyGrid.Children.Add(new Label
> {
> Text = "original row",
> TextColor = Color.Black,
> LineBreakMode = Xamarin.Forms.LineBreakMode.WordWrap,
> HorizontalTextAlignment = TextAlignment.End,
> FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
> }, 0, 0); //Column / Row
...
> MyGrid.RowDefinitions.Insert(0,newDefinition); // Insert new row
> at 0 row index
>
>
> MyGrid.Children.Add(new Label
> {
> Text = "new row",
> TextColor = Color.Black,
> LineBreakMode = Xamarin.Forms.LineBreakMode.WordWrap,
> HorizontalTextAlignment = TextAlignment.End,
> FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
> }, 0, 0); //Column / Row
"new row" will overlap "original row"
EDIT: So far, that's what I've done. This is only for one row shift,no column shift.
I could not get Grid Child column/row by
var left = Grid.Children[0].Left();//Experimental flag
So I will have to iterate more.
... add new Rows with Labels, 0 column (By default, Grid has 1 Column, 1 Grid), then:
Grid.RowDefinitions.Add(newRow);
for (int i = Grid.Children.Count -1 ; i >= 0; i--)
{
var child = > Grid.Children[i];
Grid.Children.RemoveAt(i);
Grid.Children.Add(child, 0, i +1);
}
Grid.Children.Add(SomeLabel, 0, 0);
To shift all the rows in a grid by one, Grid.GetRow() and Grid.SetRow() methods can be used like the sample code below.
foreach (var child in grid.Children)
{
var row = Grid.GetRow(child);
Grid.SetRow(child, row + 1);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With