WPF TreeView 이벤트를 두 번 선택하여 TreeView의 부모 노드에 대한 해결 방법을 가져옵니다.

1642 단어 treeview
1. TreeView에서 이벤트를 선택하여 두 번 수행
Very often, we need to execute some code in  SelectedItemChanged  depending on the selected  TreeViewItem . But SelectedItemChanged  is called twice. This is due to stealing focus from the main window, which is screwing something up.
What we have to do to avoid this is simply delay the call to our code, i.e.,  MyFunction()  which we need to execute in SelectedItemChanged . Here's a workaround which delays the call to open the new window until the item selection code finishes up:
 
private delegate void NoArgDelegate();
 
void Window1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
  Dispatcher.BeginInvoke(DispatcherPriority.Background, 
        (NoArgDelegate)delegate { MyFunction(); });
}

2. Treeview 상위 노드 가져오기
   private void treeView1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            //  (          )
            TreeViewItem item = treeView1.SelectedItem as TreeViewItem;
 
            //     
            TreeViewItem parent = item.Parent as TreeViewItem;
            //         
            if (parent != null) {
                //       ,     Header   
                MessageBox.Show("    Header:" + parent.Header.ToString());
            } else {
                MessageBox.Show("     !");
            }
 
        }

  

좋은 웹페이지 즐겨찾기