迭代动态生成的控件并获取某些属性
我有一个带有 ItemsControl 的 UserControl。此 ItemsControl 绑定到对象列表。为了显示这些对象,我有 *DataTemplate* 来确定对象的呈现方式。
我希望能够检索控件的当前值,例如 Text(如果是 TextBox)或 SelectedItem(如果是 ComboBox)。
如何迭代这些动态生成的控件?我不知道控件的名称,因为它们是在运行时生成的。
谢谢..
I have a UserControl that has an ItemsControl. This ItemsControl is bound to a list of objects. To display those objects, I have *DataTemplate*s that determine how the object is presented.
I want to be able to retrieve the current values of the controls, such as Text if it is a TextBox, or SelectedItem if it is a ComboBox.
How do I iterate over these dynamically generated controls? I do not know the names of the controls, as they are generated during runtime.
Thanks..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除了在可视化树中搜索控件及其模板属性之外,我能想到的最简单的原因可能是将 ItemsControl 数据源绑定到 ObservableCollection。这就是为什么您可以动态地将任何类型的控件添加到列表中。
完成此操作后,您可以轻松检查列表中每个 UIElement 的类型。例如;如果它是文本框,则通过转换等获取文本属性。
The simplest why I could think of besides searching the visual tree for the controls and theirs template properties is probably to bind the ItemsControl data source to an ObservableCollection. This why you can dynamically add any type of controls to the list.
Once you do that, you can easily check the type of each UIElement within the list. e.g; if it's a Textbox then get the text property by casting, etc.