adding child nodes to a treeview control in wpf,c#

Posted by ebhakt on Stack Overflow See other posts from Stack Overflow or by ebhakt
Published on 2010-03-30T11:10:42Z Indexed on 2010/03/30 11:13 UTC
Read the original article Hit count: 314

Filed under:

Hi ,

i have implemented a treeview control on a buttonclick event like this:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Collections.ObjectModel;

namespace TaxonomyTreeview { /// /// Interaction logic for Window1.xaml /// public partial class Window1 : Window { ObservableCollection _TaxonomyCollection = new ObservableCollection();

    public Window1()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {

    }

    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 :

    <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>

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

© Stack Overflow or respective owner

Related posts about treeview