迭代动态生成的控件并获取某些属性

发布于 2025-01-06 11:32:31 字数 242 浏览 2 评论 0原文

我有一个带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑白记忆 2025-01-13 11:32:31

除了在可视化树中搜索控件及其模板属性之外,我能想到的最简单的原因可能是将 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文