我正在做一个对象的DependencyProperty
回调(PropertyChangedCallback
)。我需要在代码中访问包含的代码。sender
ListBoxItem
ListBox
ListBoxItem
可能吗 ?
我已经尝试过listBoxItem.Parent
但是null
试试这个:
private void SomeEventHandler(object sender, RoutedEventArgs e)
{
ListBoxItem lbi = sender as ListBoxItem;
ListBox lb = FindParent<ListBox>(lbi);
}
private static T FindParent<T>(DependencyObject dependencyObject) where T : DependencyObject
{
var parent = VisualTreeHelper.GetParent(dependencyObject);
if (parent == null) return null;
var parentT = parent as T;
return parentT ?? FindParent<T>(parent);
}
FindParent<ListBox>
应该ListBox
在视觉树中找到父项。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句