在运行时如何将wpf控件添加到特定的网格行和单元格? [英] How to add wpf control to particular grid row and cell during runtime?

查看:124
本文介绍了在运行时如何将wpf控件添加到特定的网格行和单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的WPF窗口中有以下网格(是类窗口);

 < Grid Name = RequiredGrid > 
< Grid.ColumnDefinitions>
< ColumnDefinition Width =70/>
< ColumnDefinition Width =*/>
< /Grid.ColumnDefinitions>
< / Grid>

根据传入窗口的内容,我想一次将这些项目添加到此网格中一行。也就是说,我想在左边的列中添加一个Label,在右边的列中添加一个TextBox。我相信我知道如何通过在代码中执行以下操作来添加新行来保存新数据:

  RequiredGrid.RowDefinitions。 Add(new RowDefinition()); 

问题是,创建了我的标签和我的文本框后。

 标签AttrLabel = new Label(); 
TextBox AttrTextBox = new TextBox();

我真的不知道如何让它进入窗口,以便显示。我看到一些线程说,做这样的事情:

  this.Controls.Add(AttrLabel); 
this.Controls.Add(AttrTextBox);

这有两个问题。 1)我的窗口类没有这个控件属性或其他。 2)这不会帮助我指定每个UI项目的行和列。



现在在XAML中,Id很容易用类似的方式指定行和列这个:

 < Label Grid.Column =0Grid.Row =0/> 

虽然这样做会击败我的意图的动态性。有没有人知道如何让我的动态创建的UI元素显示在我的窗口中,并指定它将显示在网格中的哪一行和列。

解决方案

Grid.Row和Grid.Column属性是附加属性,因此不会像普通的.net属性那样设置。从代码中设置它们的正确方法是:

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

您应该可以在将它们添加到Grid对象的Children集合之前或之后进行,但设置在添加控件之前,他们应该防止任何可能的闪烁。


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.

解决方案

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:

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.

这篇关于在运行时如何将wpf控件添加到特定的网格行和单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆