Hi
Is there a way for the Tile Layout to not have overlaps on the components in the Tile Layout
so that they cant be stacked on top of each other and moved over another component
so not like this:
Hi
Is there a way for the Tile Layout to not have overlaps on the components in the Tile Layout
so that they cant be stacked on top of each other and moved over another component
so not like this:
There is no built-in collision prevention in RadzenTileLayout at the moment. While moving or resizing, the tile is only clamped to the grid boundaries (Columns/Rows), not to the other tiles, so tiles can end up on top of each other.
You can reject overlapping drops in your own code, because the layout commits the new position to your bound Col/Row/ColSpan/RowSpan values first and then raises Change. Keep a snapshot of the last valid positions and restore them when the changed tile intersects another one:
<RadzenTileLayout Columns="12" Rows="6" RowHeight="80" EditMode="true" Change=@OnChange>
@foreach (var tile in tiles)
{
<RadzenTileLayoutItem @key=@tile.Id Title=@tile.Title
@bind-Col=@tile.Col @bind-Row=@tile.Row
@bind-ColSpan=@tile.ColSpan @bind-RowSpan=@tile.RowSpan>
...
</RadzenTileLayoutItem>
}
</RadzenTileLayout>
@code {
class Tile
{
public int Id { get; set; }
public string Title { get; set; }
public int Col { get; set; }
public int Row { get; set; }
public int ColSpan { get; set; } = 1;
public int RowSpan { get; set; } = 1;
public bool Overlaps(Tile other) =>
Col < other.Col + other.ColSpan && other.Col < Col + ColSpan &&
Row < other.Row + other.RowSpan && other.Row < Row + RowSpan;
}
List<Tile> tiles = new() { /* ... */ };
Dictionary<int, (int Col, int Row, int ColSpan, int RowSpan)> lastValid = new();
protected override void OnInitialized()
{
foreach (var tile in tiles)
{
lastValid[tile.Id] = (tile.Col, tile.Row, tile.ColSpan, tile.RowSpan);
}
}
void OnChange(RadzenTileLayoutItem item)
{
var tile = tiles.First(t => t.Col == item.Col && t.Row == item.Row
&& t.ColSpan == item.ColSpan && t.RowSpan == item.RowSpan);
if (tiles.Any(other => other != tile && tile.Overlaps(other)))
{
(tile.Col, tile.Row, tile.ColSpan, tile.RowSpan) = lastValid[tile.Id];
}
else
{
lastValid[tile.Id] = (tile.Col, tile.Row, tile.ColSpan, tile.RowSpan);
}
}
}
When the restored values are passed back to the tile it snaps back to its previous cell. We will consider adding a built-in option that blocks overlapping while dragging in a future version.
UPDATE:
We have added an AllowOverlap parameter for the next release early next week. When it is set to false a move or resize that would place the tile over another tile is blocked and the tile stays at its last valid cell (or slides along the free axis), so tiles can no longer be stacked. Tiles that already overlap can still be dragged apart. The default stays true to keep the current behavior.
<RadzenTileLayout Columns="12" Rows="6" EditMode="true" AllowOverlap="false">
...
</RadzenTileLayout>