如何获取堆栈面板所在的列表框的名称?
堆栈面板的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
更新(工作)答案
您需要走上可视化树 与
StackPanel
上的VisualTreeHelper.GetParent
,如有必要,递归进一步的父级,直到找到ListBox (使用
as
或is
运算符来检测您是否已找到它)。为了更加方便,或者如果您需要沿其他方向遍历树(这有点复杂),您可以使用以 LINQy 格式公开遍历的包装器,例如 这个。
综上所述,您可能还想看看 如何按名称或查找 WPF 控件输入?。
Updated (working) answer
You need to walk up the visual tree with
VisualTreeHelper.GetParent
on theStackPanel
, and if necessary recurse over further parents, until you find theListBox
(use theas
oris
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?.
使用 .Parent 属性获取当前对象所在的对象。
Use the .Parent property to get the object that the current object is hosted in.
根据这一点,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.