从ListBoxItem获取ListBox对象

Syl*_* B. 1 .net c# wpf dependency-properties

我正在处理一个DependencyProperty回调(PropertyChangedCallback),其中sender是一个ListBoxItem对象.我需要在代码中访问ListBox包含的代码ListBoxItem.

可能吗 ?

我试过listBoxItem.Parent但是确实如此null

Mar*_*rno 5

答案是:

VisualTreeHelper.GetParent(listBoxItem);
Run Code Online (Sandbox Code Playgroud)

澄清:

VisualTreeHelper.GetParent(visualObject);
Run Code Online (Sandbox Code Playgroud)

为您提供给定视觉对象的直接父级.

这意味着如果你想要ListBox给定的ListBoxItem,由于直接父 ListboxItem元素是ItemsPanel属性指定的Panel元素,你将不得不重复它,直到你得到它ListBox.