获取 StackPanel 内 UserControl 的内容

发布于 2024-12-19 01:07:24 字数 335 浏览 5 评论 0原文

我在 WPF 页面中有一个 StackPanel,并且根据用户请求在其中加载相同的 UserControl。现在,对于每个用户控件,我需要获取其中的项目。 UserControl 由一个日历和 3 个组合框组成。我怎样才能获得物品?

到目前为止,我有以下内容:-

foreach (UserControl child in stk.Children)
{

}

子项具有所需的内容,但是我无法找到如何获取每个子项的内容以进行验证并将其存储在数据库中。我尝试了 child.FindControl("combobox1") 但这不起作用。

感谢您的帮助和时间

I have a StackPanel inside a WPF page, and I am loading the same UserControl inside it depending on the user requests. Now for each of these UserControls, I need to get the items which are inside it. The UserControl consists of a Calendar, and 3 comboboxes. How can I get the items?

So far I have the following:-

foreach (UserControl child in stk.Children)
{

}

and child has the contents required, however I cannot find out how to get the contents of every child to validate and store in the database. I tried child.FindControl("combobox1") but that did not work.

Thanks for your help and time

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

莫言歌 2024-12-26 01:07:24

您可以获取 Content 。不过,我强烈建议您不要在 UI 中执行此类操作,并且您不应该从 UI 中获取数据。 UI 应该从 ViewModel 或 Model 获取数据,并在需要时更新它们(这是通过 Binding 实现的)。因此,如果您需要数据,您的 ViewModel 应该是查看的地方,而不是 UI。

You can get hold of the Content . However I strongly suggest you don't do those of kind of things in the UI, and you shouldn't get the data from the UI. the UI should get the data from a ViewModel or a Model, and update them when needed(This is achieved through Binding). so if you need the data, your ViewModel should be the place to look at, not the UI.

空气里的味道 2024-12-26 01:07:24

我发现以下线程非常有帮助!

如何按名称或类型查找 WPF 控件?

它使用助手来查找所有WPF 页面中的控件。

感谢大家的帮助

I have found the following thread which was very helpful!

How can I find WPF controls by name or type?

It uses a helper to find all the controls in a WPF page.

Thanks for all your help guys

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