WPF creating grid from XAML in code-behind

萝らか妹 提交于 2019-12-13 10:57:45

问题


I have an empty grid with no data but with all the columns in the XAML - is there way to create multiple of these grids in the code-behinds ie .cs file

I know how to create new grids in the code-behind but not existing grids...any ideas?

Thanks Ram


回答1:


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);
}


来源:https://stackoverflow.com/questions/43499763/wpf-creating-grid-from-xaml-in-code-behind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!