如何在运行时将 wpf 控件添加到特定的网格行和单元格?
我的 WPF“窗口”中有以下网格(是的类窗口);
<Grid Name="RequiredGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="70" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
</Grid>
根据传递到窗口中的内容,我想一次一行地将项目添加到此网格中。也就是说,我想在左列中添加一个标签,在右列中添加一个文本框。我相信我知道如何通过在后面的代码中执行以下操作来添加新行来保存新数据:
RequiredGrid.RowDefinitions.Add(new RowDefinition());
问题是,在我创建标签和文本框之后。
Label AttrLabel = new Label();
TextBox AttrTextBox = new TextBox();
我真的不知道如何将其放入窗口中以便显示。我见过一些线程说,做这样的事情:
this.Controls.Add(AttrLabel);
this.Controls.Add(AttrTextBox);
这有两个问题。 1)我的Window类没有这个“Controls”属性或其他属性。 2)这不会帮助我指定每个 UI 项目的行和列。
现在,在 XAML 中,我可以轻松地使用如下所示的内容来指定行和列:
<Label Grid.Column="0" Grid.Row="0"/>
不过,这违背了我的意图的“动态性”。有谁知道如何让动态创建的 UI 元素显示在我的窗口中,并指定它将显示在网格中的行和列。
I have the following grid in my WPF "Window" (yes the class Window);
<Grid Name="RequiredGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="70" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
</Grid>
Depending on whats passed into the window, I want to add items into this grid one row at a time. Namely, I want to add a Label in the left column and a TextBox in the right column. I believe I know how to add new rows for holding new data by doing the following in the code behind:
RequiredGrid.RowDefinitions.Add(new RowDefinition());
Problem is, after I've created my Label and my TextBox.
Label AttrLabel = new Label();
TextBox AttrTextBox = new TextBox();
I don't really know how to get it into the Window so it gets displayed. I've seen some threads that say, do something like this:
this.Controls.Add(AttrLabel);
this.Controls.Add(AttrTextBox);
There are two problems with this. 1) My Window class doesn't have this "Controls" property or whatever. And 2) This wouldn't help me specify the row and column of each UI item.
Now in XAML, Id be easy to specify the row and column with something like this:
<Label Grid.Column="0" Grid.Row="0"/>
This defeats the "dynamic-ness" of my intent here though. Does anyone know how I can get my dynamicaly created UI elements to display in my Window and specify which row and column it will show up in the grid.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Grid.Row 和 Grid.Column 属性是附加属性,并且作为这些属性的设置方式与普通 .net 属性不同。从代码中设置它们的正确方法是:
您应该能够在将它们添加到 Grid 对象的 Children 集合之前或之后执行此操作,但在添加控件之前设置它们应该可以防止任何可能的闪烁。
The Grid.Row and Grid.Column properties are Attached Properties, and as such are not set like normal .net properties. The right way to set them from code is:
You should be able to do this before or after adding them to the Grid object's Children collection, but setting them before adding the control should prevent any possible flickering.
) 和行定义。创建控件 (
)。然后设置网格的 ColumnSpan 和 Row:Grid.SetColumnSpan(, 3);, 0);
Grid.SetRow(
然后将控件添加到您创建的网格
<yourGrid>
) and the Row Definitions like you have done.Create The Control (
<yourcontrol>
). Then Set The ColumnSpan and Row for the Grid:Grid.SetColumnSpan(<yourControl>, 3);
Grid.SetRow(<yourControl>, 0);
Then add your control to the grid you have created
<yourGrid>.Children.Add(<yourControl>);