Handles the Selected event for all TreeViewItems : TreeView « Windows Presentation Foundation « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Windows Presentation Foundation » TreeViewScreenshots 
Handles the Selected event for all TreeViewItems
Handles the Selected event for all TreeViewItems
  
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="200" Width="300">
    <DockPanel LastChildFill="True">
        <DockPanel.Resources>
            <Style TargetType="{x:Type TreeViewItem}">
                <EventSetter Event="Selected" Handler="TreeViewItem_Selected" />
            </Style>
        </DockPanel.Resources>
        <Button Click="Button_Click" DockPanel.Dock="Bottom" Content="Show Selected" MaxHeight="23" MaxWidth="100" />
        <TreeView FontSize="16" Name="tvTree">
            <TreeViewItem Header="A" IsExpanded="True">
                <TreeViewItem Header="1">
                    <TreeViewItem Header="2" />
                    <TreeViewItem Header="3" />
                </TreeViewItem>
                <TreeViewItem Header="B" IsExpanded="True">
                    <TreeViewItem Header="11" />
                    <TreeViewItem Header="22" />
                </TreeViewItem>
            </TreeViewItem>
            <TreeViewItem Header="C">
                <TreeViewItem Header="E">
                    <TreeViewItem Header="111" />
                    <TreeViewItem Header="222" />
                    <TreeViewItem Header="333" />
                </TreeViewItem>
                <TreeViewItem Header="F">
                    <TreeViewItem Header="1111" />
                    <TreeViewItem Header="2222" />
                    <TreeViewItem Header="333" />
                </TreeViewItem>
            </TreeViewItem>
        </TreeView>
    </DockPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void TreeViewItem_Selected(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = sender as TreeViewItem;
            if (item == e.OriginalSource)
            {
                Console.WriteLine(item.Header);
                Console.WriteLine(item.Items.Count);
            }
            else
            {
                Console.WriteLine("Parent of selected");
                Console.WriteLine(item.Header);
                Console.WriteLine(item.Items.Count);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = tvTree.SelectedItem as TreeViewItem;

            if (item != null)
            {
                MessageBox.Show("Item selected: " + item.Header, Title);
            }
            else
            {
                MessageBox.Show("No item selected", Title);
            }
        }
    }
}

   
    
  
Related examples in the same category
1.TreeView uses SelectedValuePath property to provide a SelectedValue for the SelectedItem.TreeView uses SelectedValuePath property to provide a SelectedValue for the SelectedItem.
2.TreeView with DataSourceTreeView with DataSource
3.TreeView And TreeViewItemTreeView And TreeViewItem
4.Create a Tree and add childrenCreate a Tree and add children
5.Add Ellipse, TextBlock to a TreeAdd Ellipse, TextBlock to a Tree
6.Mark Tree node expandableMark Tree node expandable
7.Bind a TreeView to a data source and use DataTemplate objects to customize the TreeView.Bind a TreeView to a data source and use DataTemplate objects to customize the TreeView.
8.Set TreeView with TreeViewItemSet TreeView with TreeViewItem
9.View and Select Items Using a TreeView and Select Items Using a Tree
10.Get selected tree node itemGet selected tree node item
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.