如何获取堆栈面板所在的列表框的名称?

发布于 2024-12-21 21:49:02 字数 151 浏览 3 评论 0原文

堆栈面板的 DragOver 事件使我可以作为发送者访问堆栈面板,但如何获取堆栈面板所在的列表框的名称(堆栈面板在数据模板中定义)。这样做的原因是最终访问数据模板生成的列表中的所有其他堆栈面板,因此如果我在这里指出错误的树,我将欣赏访问其他堆栈面板的替代方法。谢谢,

丹。

A stackpanel's dragOver event gives me access to the stackpanel as sender, but how can I get the name of the listbox that the stackpanel is in (stack panel is defined in the datatemplate). The reason for this is to ultimately access all of the other stackpanels in the list generated by the datatemplate so if I'm barking up the wrong tree here I'll appreciate an alternative way of accessing the other stackpanels. Thanks,

Dan.

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

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

发布评论

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

评论(4

随遇而安 2024-12-28 21:49:02

更新(工作)答案

您需要走上可视化树StackPanel 上的VisualTreeHelper.GetParent,如有必要,递归进一步的父级,直到找到 ListBox (使用 asis 运算符来检测您是否已找到它)。

为了更加方便,或者如果您需要沿其他方向遍历树(这有点复杂),您可以使用以 LINQy 格式公开遍历的包装器,例如 这个

综上所述,您可能还想看看 如何按名称或查找 WPF 控件输入?

Updated (working) answer

You need to walk up the visual tree with VisualTreeHelper.GetParent on the StackPanel, and if necessary recurse over further parents, until you find the ListBox (use the as or is operator to detect that you have found it).

For added convenience, or if you need to traverse the tree in other directions (which is a bit more involved), you can use a wrapper that exposes the traversal in a LINQy format such as this one.

All this said, you might also want to take a look at How can I find WPF controls by name or type?.

心意如水 2024-12-28 21:49:02

使用 .Parent 属性获取当前对象所在的对象。

Use the .Parent property to get the object that the current object is hosted in.

全部不再 2024-12-28 21:49:02
var listboxName = (stackpanel.Parent as ListBox).Name;
var listboxName = (stackpanel.Parent as ListBox).Name;
伊面 2024-12-28 21:49:02

根据这一点,StackPanel 将无法到达由于逻辑树位于模板内部,因此通过 Parent 属性确定逻辑树的根,因此请使用 TemplatedParent 属性。在其中您可能会得到一个 ListBoxItem,它将列表框作为其父项。

According to this the StackPanel will not reach the root of the logical tree by the Parent property as it is located inside a template, so use the TemplatedParent property. In it you will probably get a ListBoxItem, which will have listbox as its parent.

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