none
C#在用treeview控件时怎样设置点击节点时跳到我想要的form RRS feed

全部回复

  • 你好,

    WPFTreeView中,可以使用SelectedItemChanged 事件来完成,

    <TreeView SelectedItemChanged="SelectionChanged">
      <TreeViewItem Header="Employee1" IsSelected="True">
        <TreeViewItem Header="Jesper"/>
        <TreeViewItem Header="Aaberg"/>
        <TreeViewItem Header="12345"/>
      </TreeViewItem>
      <TreeViewItem Header="Employee2">
        <TreeViewItem Header="Dominik"/>
        <TreeViewItem Header="Paiha"/>
        <TreeViewItem Header="98765"/>
      </TreeViewItem>
    </TreeView>

    private void SelectionChanged(object sender, RoutedPropertyChangedEventArgs<Object> e) { //Perform actions when SelectedItem changes

    }

    谢谢~

    The future belongs to those who believe in the beauty of their dreams.

    2017年7月31日 2:48
    版主

  • Hi 孙晓杰,

    >>C#在用treeview控件时怎样设置点击节点时跳到我想要的form

    你也可以尝试使用TreeViewItem.Selected 事件,来获取每个节点的信息,然后跳转到你想要的界面. 下面的代码供你参考,希望对你有所帮助。

    XAML:

      <Grid Margin="10">
            <TreeView Name="trvFamilies"  TreeViewItem.Selected="trvFamilies_Selected">
                <TreeView.Resources>
                    <HierarchicalDataTemplate DataType="{x:Type local:Family}" ItemsSource="{Binding Members}">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Name}" />
                            <TextBlock Text=" [" Foreground="Blue" />
                            <TextBlock Text="{Binding Members.Count}" Foreground="Blue" />
                            <TextBlock Text="]" Foreground="Blue" />
                        </StackPanel>
                    </HierarchicalDataTemplate>
                    <DataTemplate DataType="{x:Type local:FamilyMember}">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Name}" />
                            <TextBlock Text=" (" Foreground="Green" />
                            <TextBlock Text="{Binding Age}" Foreground="Green" />
                            <TextBlock Text=" years)" Foreground="Green" />
                        </StackPanel>
                    </DataTemplate>
                    
                </TreeView.Resources>
            </TreeView>
    

    XAML.CS:

          private void trvFamilies_Selected(object sender, RoutedEventArgs e)
            {
                TreeViewItem tvi = e.OriginalSource as TreeViewItem;
                if (tvi.HasItems)
                {
                    Family folder = (Family)tvi.Header;
                    string id = folder.FamilyID.ToString();
                    string Name = folder.Name;
                    MessageBox.Show(id + " - " + Name);
                    yourwindows TB = new yourwindows();
                    TB.ShowDialog();
                }
                else
                {
                    FamilyMember folder = (FamilyMember)tvi.Header;
                    string id = folder.FamilyID.ToString();
                    string Name = folder.Name;
                    MessageBox.Show(id + " - " + Name);
                }
    
               
            }
    

    TreeViewItem.Selected Event:
    https://msdn.microsoft.com/en-us/library/system.windows.controls.treeviewitem.selected(v=vs.110).aspx

    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年7月31日 3:16
    版主