Wpf-如何获取触发 TreeViewItem.Loaded 事件的 TreeViewItem?
我试图在加载时将一些处理程序附加到每个 TreeViewItem
,然后在卸载时删除处理程序。
这是我的自定义控件中继承自 TreeView
的代码:
public ModdedTreeView()
{
this.AddHandler(TreeViewItem.LoadedEvent, new RoutedEventHandler(ItemLoaded));
this.AddHandler(TreeViewItem.UnloadedEvent, new RoutedEventHandler(ItemUnloaded));
}
protected void ItemLoaded(object sender, RoutedEventArgs e)
{
TreeViewItem item = e.OriginalSource as TreeViewItem;
if (item == null)
return;
item.AddHandler(TreeViewItem.CollapsedEvent, new RoutedEventHandler(ItemCollapsed));
item.AddHandler(TreeViewItem.ExpandedEvent, new RoutedEventHandler(ItemExpanded));
item.AddHandler(TreeViewItem.SelectedEvent, new RoutedEventHandler(ItemSelected));
}
protected void ItemUnloaded(object sender, RoutedEventArgs e)
{
TreeViewItem item = e.OriginalSource as TreeViewItem;
if (item == null)
return;
item.RemoveHandler(TreeViewItem.CollapsedEvent, new RoutedEventHandler(ItemCollapsed));
item.RemoveHandler(TreeViewItem.ExpandedEvent, new RoutedEventHandler(ItemExpanded));
item.RemoveHandler(TreeViewItem.SelectedEvent, new RoutedEventHandler(ItemSelected));
}
编辑:
我仍然不知道发生了什么。它似乎只是拾取 TreeView
加载事件而不是 TreeViewItem
加载事件。
I am trying to attach some handlers to each TreeViewItem
as they are loaded and then remove the handlers as they are unloaded.
Here is the Code that I have in my custom control which inherits from TreeView
:
public ModdedTreeView()
{
this.AddHandler(TreeViewItem.LoadedEvent, new RoutedEventHandler(ItemLoaded));
this.AddHandler(TreeViewItem.UnloadedEvent, new RoutedEventHandler(ItemUnloaded));
}
protected void ItemLoaded(object sender, RoutedEventArgs e)
{
TreeViewItem item = e.OriginalSource as TreeViewItem;
if (item == null)
return;
item.AddHandler(TreeViewItem.CollapsedEvent, new RoutedEventHandler(ItemCollapsed));
item.AddHandler(TreeViewItem.ExpandedEvent, new RoutedEventHandler(ItemExpanded));
item.AddHandler(TreeViewItem.SelectedEvent, new RoutedEventHandler(ItemSelected));
}
protected void ItemUnloaded(object sender, RoutedEventArgs e)
{
TreeViewItem item = e.OriginalSource as TreeViewItem;
if (item == null)
return;
item.RemoveHandler(TreeViewItem.CollapsedEvent, new RoutedEventHandler(ItemCollapsed));
item.RemoveHandler(TreeViewItem.ExpandedEvent, new RoutedEventHandler(ItemExpanded));
item.RemoveHandler(TreeViewItem.SelectedEvent, new RoutedEventHandler(ItemSelected));
}
Edit:
I still can not figure out what is going on. It just seems to be picking up the TreeView
loaded event instead of the TreeViewItem
loaded event.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Loaded 是一个路由事件,因此您可以在
OriginalSource
属性中找到其来源,该属性应该是触发该事件的 TreeViewItem。Loaded is a routed event, so you will find its source in the
OriginalSource
property, which should be the treeviewitem that triggered the event.显然 TreeViewItem.Loaded 事件是一个直接事件。所以没有办法完成我正在尝试的事情。
Apparently the TreeViewItem.Loaded event is a direct event. So there is no way to accomplish what I was trying.