Search code examples
.netwpfwpf-controls.net-5

How to add new rows at top programmatically in WPF grid (.NET5)


I am using WPF with .NET5 to develop a Windows app where I am using Grid to display some content, and I need to programmatically add a new row at the top of the grid.

One method I considered is modifying row number of each existing row but it's a tedious task. I am using grid to display the content as grid expands dynamically with the expansion of the main window whereas as far as I know, other containers do not show this behavior.

Another method I considered is inserting a row using gridName.Childern.Insert() but it inserts and overlaps the existing row.

I need a solution to either dynamically add a new row at top of the grid without overlapping or to make other containers such as Stack Panel and Dock Panel expand while maximizing the main window.

Is there any other efficient way of achieving this functionality?


Solution

  • This is very annoying problem.

    You have to change all Grid.Row, Grid.Column

    But You can choice other thins

    if you need only one orientation, you can use StackPanel

    if not, You should use WrapPanel