c# - 从 ListBoxItem 获取 ListBox 对象

标签 c# .net wpf dependency-properties

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

这可能吗?

我试过 listBoxItem.Parent 但它是 null

最佳答案

答案是:

VisualTreeHelper.GetParent(listBoxItem);

澄清一下:

VisualTreeHelper.GetParent(visualObject);

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

这意味着如果您想要给定 ListBoxItemListBox,因为 ListboxItem 的直接父级是由ItemsPanel 属性,您将不得不重复它直到获得 ListBox

关于c# - 从 ListBoxItem 获取 ListBox 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52167105/

相关文章:

c# - 使用表中的外键在连接到MYSQL的C#DatagridView中插入行

c# - 有没有办法调用通用的methodInfo?

c# - MSMQ 服务未读取队列

wpf - 从 VSTO Outlook 加载项生成的居中 WPF 对话框

c# - 在 Silverlight/WPF 中将元素前移(Z 索引)

c# - 有没有办法在 Windows 10 中使用蓝牙 LE 库 C# 与传感器或微 Controller 进行通信?

.net - 使用 Flash 或 .NET 以移动图像作为叠加层的视频

c# - Task.WhenAny 取消未完成的任务和超时

c# - 命令绑定(bind)不起作用

c# - XPath 查询不起作用