[Solved] WPF creating grid from XAML in code-behind


You can do that by making your “existing grid” a separate UserControl.

First, you need to add a UserControl via [Add]->[User Control…]->[User Control (WPF)].

Next, put your “existing grid” inside the added UserControl.

YourExistingGridControl.xaml

<UserControl x:Class="Your.Namespace.YourExistingGridControl">
  <Grid>
     ... YOUR EMPTY GRID WITH ALL THE COLUMNS, ETC. ...
  </Grid>
</UserControl>

Now, you can create as many instances of that “existing grid” as you like.

YourCodeBehind.xaml.cs

private void AddYourExistingGrid()
{
  var myGrid = new Your.Namespace.YourExistingGridControl();

  yourWrapPanel.Children.Add(myGrid);
}

0

solved WPF creating grid from XAML in code-behind