Hi ,
i have implemented a treeview control on a buttonclick event like this:
namespace TaxonomyTreeview
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
ObservableCollection<TaxonomyData> _TaxonomyCollection = new ObservableCollection<TaxonomyData>();
public Window1()
{
InitializeComponent();
}
public ObservableCollection<TaxonomyData> TaxonomyCollection
{ get { return _TaxonomyCollection; } }
private void SelectedTaxonomyChanged(object sender,
RoutedPropertyChangedEventArgs<Object> e)
{
TaxonomyData taxo = taxonomytree.SelectedItem as TaxonomyData;
if (taxo != null)
{
MessageBox.Show("" + taxo.Tid);
}
}
public class TaxonomyData
{
private string _name;
private string _tid;
public string Tid
{
get { return _tid; }
set { _tid = value; }
}
public string Name
{
get { return _name; }
set { _name = value; }
}
public TaxonomyData(string name, string tid)
{
Name = name;
Tid = tid;
}
}
private void populate_Click(object sender, RoutedEventArgs e)
{
taxonomytree.Items.Clear();
TaxonomyCollection.Add(new TaxonomyData("Taxonomy1", "1"));
taxonomytree.Items.Add(TaxonomyCollection[0]);
}
}
}
The xaml code is :
<Window x:Class="TaxonomyTreeview.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="435" Width="458" Loaded="Window_Loaded">
<Grid>
<TextBox Height="23" Margin="20,9,0,0" Name="startvid" VerticalAlignment="Top" HorizontalAlignment="Left" Width="120" />
<TextBox Height="23" Margin="165,9,151,0" Name="endvid" VerticalAlignment="Top" />
<Button Height="23.78" HorizontalAlignment="Right" Margin="0,8.22,11,0" Name="populate" VerticalAlignment="Top" Width="115" Click="populate_Click">Populate</Button>
<TreeView HorizontalAlignment="Left" Margin="20,53,0,144" Width="120" Name="taxonomytree" ItemsSource="{Binding Window1.TaxonomyCollection}"
SelectedItemChanged="SelectedTaxonomyChanged">
<TreeView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}" />
</DataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Grid>
</Window>
As i want to display, the structure in a hierarchy, i want to know what is the best method to add a child node to this
Please help