通过 FindName() 查找动态生成的 Wcf 控件
在父控件中查找控件的 FramworkElement.FindName() 方法似乎应该直接进行...
但是我正在升级 Anty,并且框架似乎不喜欢我想要做的事情。
首先,我确实意识到有很多不同的做事方式,并且请记住这是我在 WPF 中创建的第一个表单。
我根据集合中的项目数量将控件加载到 StackPanel 中。此方法是必须的,因为集合是由[用户输入的 URL] 中的目录数量决定的。
该列表将水平堆栈面板构建为在设计期间放置在表单上的垂直堆栈面板。
所以 dirStackPanel 就在表单上。 我将 controlStackPanel 插入 dirStackPanel n 次,并使用识别名称命名每个控件:(string)("controlStackPanel" + n)。
我还用控件填充 controlStackPanel,但考虑到我无法按名称从其父级 (dirStackPanel) 检索 controlstackPanel,这是一个有争议的问题。
例如:
var getPanel = (StackPanel) this.dirStackPanel.FindName((string)("controlStackPanel" + n))返回 Null
所以要清楚的是,我的每个控件am inserting AT RUN TIME 正在被分配一个名称,并且可以使用循环轻松检索。但 FindName 方法不适用于父级“StackPanel”的第一个子控件。它不断返回 null 而不是对象。
有什么想法吗?
The FramworkElement.FindName() method of finding a control within a parent control seems like it should straight forward...
But I am upping the anty and it seems like the framework does not like what I'm trying to do.
First off, I do realize there are plenty of different ways of doing things and keep in mind this is the first form I am creating in WPF.
I am loading controls into a StackPanel based on the number of items in a collection. This method is a must as the collection is determined by the number of directories within a [user entered Url].
The list builds horizontal stackpanels into a vertical stackpanel that is placed on the form during design.
So dirStackPanel is on the form.
I am inserting controlStackPanel into dirStackPanel n number of times and name each one with an identifying name: (string)("controlStackPanel" + n).
I am also filling the controlStackPanel with controls but that is a moot point considering that I can not retrieve the controlstackPanel from its parent (dirStackPanel) by name.
example:
var getPanel = (StackPanel) this.dirStackPanel.FindName((string)("controlStackPanel" + n))
Returns Null
So to be clear, each control that I am inserting AT RUN TIME is being assigned a name and can easily be retrieved using a loop. But the FindName method will not work on the first child control of the parent "StackPanel". It keeps returning a null and not the object.
Any Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要动态创建控件,则必须首先调用 RegisterName能够找到他们。
例如:
If you are creating the controls dynamically, you have to call RegisterName first to be able to find them.
For example: