I have a treeview where I have added an eventsetter to the item container style to catch whenever a F1 is pressed while hovering a mouse over. So in the code behind I tried to find the child object that the mouse is over. The child object is only found in the tree after a node have been expanded and tried once before, the key down is catched correctly every time. So it is only the second time the IsMouseOver
child object is found.
I have disabled virtualization for the target tree, but it doesn't make any difference.
<HierarchicalDataTemplate.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<EventSetter Event="PreviewKeyDown" Handler="EventSetter_OnHandler"></EventSetter>
<Setter Property="IsSelected">
<Setter.Value>
<MultiBinding Mode="OneWay" Converter="{StaticResource ActiveReportTypeMatchToBoolConverter}">
<Binding Path="DataContext.ActiveReportType" ElementName="TreeViewExpander" />
<Binding />
</MultiBinding>
</Setter.Value>
</Setter>
<Setter Property="UIElement.Uid" Value="{Binding Name}" />
</Style>
</HierarchicalDataTemplate.ItemContainerStyle>
The Code behind event handler
private void EventSetter_OnHandler(object sender, KeyEventArgs e) {
if (e.Key == Key.F1) {
foreach (var item in TreeViewReportType.Items) {
TreeViewItem anItem = TreeViewReportType.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
if (anItem?.IsMouseOver == true) {
foreach (ReportType childItem in anItem.Items) {
TreeViewItem childTreeViewItem = anItem.ItemContainerGenerator.ContainerFromItem(childItem) as TreeViewItem;
if (childTreeViewItem?.IsMouseOver == true) {
ApplicationCommands.Help.Execute(childItem.HelpId, childTreeViewItem);
}
}
return;
}
}
}
}
Do any of you know of a magic trick here ?
I have tried to do a TreeViewReportType.UpdateLayout()
and also anItem.UpdateLayout()
to see if it made any changes. But it didn't help.
Tried to look on previous answers but it is datagrid related and is to disable virtualization, which doesn't work here ?