xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPF" Height="200" Width="300">
//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);
}
}
}
}