从ListBoxItem获取ListBox对象

西尔文·B。

我正在做一个对象DependencyProperty回调(PropertyChangedCallback我需要在代码中访问包含的代码senderListBoxItemListBoxListBoxItem

可能吗 ?

我已经尝试过listBoxItem.Parent但是null

毫米8

试试这个:

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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

获取调用当前对象的对象

尝试从ListBox对象转换为MyListBox时出现invalidcastexception:WPF中的ListBox对象

从ListBox中的数据对象获取DataTemplate

在C#中删除listBoxItem时,ListBox不刷新

从选定的ListBoxItem模板获取Textblock值

从WPF ListBox获取SelectedValue的对象ID

<ListBox.Resources>或<ListBox.ItemContainerStyle>中的ListBoxItem样式?

将WPF ListBox.ItemTemplate的内容拉伸到ListBoxItem的宽度的最佳方法

由ListBox控件自动管理的通用对象?

从UserControl获取ListBox DataContext

在WPF中,什么将ListBoxItem与ListBox相关联?

ListBox获取值/显示成员

获取VBA中所选ListBox项的顺序?

获取MouseOver上ListBoxItem的内容以出现在其他控件中

如何显示指向ListBox中的列表的对象?

WPF ListBox.SelectedItems作为ListBoxItem

从 vanilla JS 获取 paper-listbox 的值

WPF C# 从 ListBoxItem 内的 Textblock 获取文本

XAML 扩展 ListBox 和 ListBoxItem 控件

ListBox 获取选定的索引?

从 ListBox 获取完整信息

从 listBox c# 中获取随机项目

C# WPF Tab 导航以从 ListBox 外部控制 ListBoxItem 内部

WPF ListBox 使用 DataTemplate 显示对象属性

从 Python GTK 中的 ListBox(绑定到 ListStore)中获取所选对象

WPF Listbox 控件模板未显示 Listboxitem 控件模板和 ItemTemplate 数据模板

HttpClient 获取对象对象

检索与 ObservableCollection 的对象对应的 ListBoxItem

如何在 ListBox 中绑定和显示 ListBoxItem 索引?