我正在处理 DependencyProperty
回调 (PropertyChangedCallback
),其中 sender
是一个 ListBoxItem
对象。我需要在代码中访问包含 ListBoxItem
的 ListBox
。
这可能吗?
我试过 listBoxItem.Parent
但它是 null
最佳答案
答案是:
VisualTreeHelper.GetParent(listBoxItem);
澄清一下:
VisualTreeHelper.GetParent(visualObject);
为您提供给定视觉对象的直接父级。
这意味着如果您想要给定 ListBoxItem
的 ListBox
,因为 ListboxItem
的直接父级是由ItemsPanel
属性,您将不得不重复它直到获得 ListBox
。
关于c# - 从 ListBoxItem 获取 ListBox 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52167105/