Ghost Parent TreeView!

I have a TreeView that launches a new window when each of its TreeViewItems Selected events is fired.

<Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"> <Grid> <TreeView Name="treeView1"> <TreeViewItem Header="Root"> <TreeViewItem Header="Parent" Selected="ParentTreeViewItem_Selected"> <TreeViewItem Header="Child" Selected="TreeViewItem_Selected" ></TreeViewItem> </TreeViewItem> </TreeViewItem> </TreeView> </Grid> </Window> 

Code for

 namespace WpfApplication1 

{Open partial class Window1: Window {public Window1 () {InitializeComponent (); }

  private void TreeViewItem_Selected(object sender, RoutedEventArgs e) { Window w = new Window(); w.Show(); e.Handled = true; } private void ParentTreeViewItem_Selected(object sender, RoutedEventArgs e) { } } 

}

When I click on a child of a node, a new window starts as expected. Be that as it may, after the words of his parents, the selected events launched the focus focus from a new window and marked the parent node as the current selection!

I expected that the new window would have focus, and the node that was clicked would turn gray, indicating a choice of users. Does anyone know why this is happening and how can I prevent it?

Thanks Brette

+4
source share
2 answers

I think I will post the answer. Finally, I found a way around this. Setting w.Owner = this; has no effect. It turns out that starting a new window in the selected TreeViewItem event causes some focus problems. I have not figured out what is the main reason for this, doing it on the Manager seems to fix it. See below

  private void ChildTreeViewItem_Selected(object sender, RoutedEventArgs e) { Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() => new Window().Show())); } 

Hope this saves you a little time.

Brette

+2
source

Add

 w.Owner = this 

Example:

 private void TreeViewItem_Selected(object sender, RoutedEventArgs e) { Window w = new Window(); w.Owner = this; w.Show(); e.Handled = true; } 
0
source

All Articles