简体   繁体   English

如何在运行时将wpf控件添加到特定网格行和单元格?

[英]How to add wpf control to particular grid row and cell during runtime?

I have the following grid in my WPF "Window" (yes the class Window); 我的WPF“窗口”中有以下网格(是类窗口);

<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. 也就是说,我想在左列中添加一个Label,在右列中添加一个TextBox。 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和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. 1)我的Window类没有这个“Controls”属性或其他什么。 And 2) This wouldn't help me specify the row and column of each UI item. 2)这无助于我指定每个UI项的行和列。

Now in XAML, Id be easy to specify the row and column with something like this: 现在在XAML中,我很容易用这样的东西指定行和列:

 <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. 有谁知道如何让我的动态创建的UI元素显示在我的窗口中,并指定它将在网格中显示的行和列。

The Grid.Row and Grid.Column properties are Attached Properties , and as such are not set like normal .net properties. Grid.Row和Grid.Column属性是附加属性 ,因此不像普通的.net属性那样设置。 The right way to set them from code is: 从代码中设置它们的正确方法是:

Grid.SetRow(someLabel, 0);
Grid.SetColumn(someLabel, 0);

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. 您应该能够在将它们添加到Grid对象的Children集合之前或之后执行此操作,但在添加控件之前设置它们应该可以防止任何可能的闪烁。

  • Create the grid ( <yourGrid> ) and the Row Definitions like you have done. 像你一样创建网格( <yourGrid> )和行定义。
  • Create The Control ( <yourcontrol> ). 创建控件( <yourcontrol> )。 Then Set The ColumnSpan and Row for the Grid: 然后为网格设置ColumnSpan和Row:

    Grid.SetColumnSpan(<yourControl>, 3); Grid.SetRow(<yourControl>, 0);

  • Then add your control to the grid you have created 然后将控件添加到您创建的网格中

    <yourGrid>.Children.Add(<yourControl>);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM