获取 StackPanel 内 UserControl 的内容
我在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以获取
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.我发现以下线程非常有帮助!
如何按名称或类型查找 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