WPF从代码隐藏的XAML创建网格 [英] WPF creating grid from XAML in code-behind
问题描述
我有一个没有数据的空网格,但包含XAML中的所有列 - 是否可以在代码隐藏中创建多个这样的网格,即.cs文件
我知道如何在代码隐藏中创建新的网格,但不存在网格......任何想法?
感谢
Ram
您可以通过将现有网格设置为单独的 UserControl
/ p>
首先,您需要通过[添加] - > [用户控制...] - >添加一个 UserControl
[用户控制(WPF)]。
接下来,将您的现有网格添加到 UserControl
中。
YourExistingGridControl.xaml
< UserControl x:Class =Your.Namespace.YourExistingGridControl>
<网格>
...您所有列的空格,等等。 ...
< / Grid>
< / UserControl>
现在,您可以随意创建现有网格的多个实例。
YourCodeBehind.xaml.cs
private void AddYourExistingGrid()
{
var myGrid = new Your.Namespace.YourExistingGridControl();
yourWrapPanel.Children.Add(myGrid);
}
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
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);
}
这篇关于WPF从代码隐藏的XAML创建网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!