Click here to Skip to main content
15,886,038 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I need to create the treeview items dynamically (here am creating in for loop), whenever i select the tree view items i want to get the label in new pane but here If i select second item in tree view items its getting overwritten in previous panels which created for first items.

Please help me.

C#
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;

namespace WpfApplication7
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            Update();
        }

        private void Update()
        {
            TreeViewItem newChild = new TreeViewItem();
            newChild.Header = "Client";
            tv.Items.Add(newChild);

            for (int i = 0; i < 2; i++)
            {
                TreeViewItem newSubChild = new TreeViewItem();
                newSubChild.Header = "name" + i;
                newChild.Items.Add(newSubChild);
                          
                newSubChild.Selected += new RoutedEventHandler(newSubChild_Selected);
               
            }
        }

        void newSubChild_Selected(object sender, RoutedEventArgs e)
        {
            Grid dp = new Grid();
            dp.Height = 20;
            dp.Width = 100;
            
            Label lbServiceName = new Label();
            lbServiceName.Content = "Service Name:";
            lbServiceName.Height = 28;
            lbServiceName.Width = 120.001;

            dp.Children.Add(lbServiceName);
            this.grd2.Children.Add(dp);         
       }               
    }
}
Posted
Updated 22-May-10 1:27am
v2

1 solution

It might be a good idea to take a good look at databinding to solve this issue.
You could create model like this:

public class MyViewModel: INotifyPropertyChanged
{
    private ObservableCollection<myhierarchicalitem> _myHierarchicalItems;
    private MyHierarchicalItem _selectedItem;

    public ObservableCollection<myhierarchicalitem>  MyHierarchicalItems
    {
        get { return _myHierarchicalItems; }
        set
        {
            _myHierarchicalItems = value;
            RaisePropertyChanged("MyHierarchicalItems");
        }
    }

    public MyHierarchicalItem SelectedItem 
    {
        get { return _selectedItem; }
        set 
        { 
            _selectedItem = value; 
            RaisePropertyChanged("SelectedItem");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName)
    {
        if(PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}</myhierarchicalitem></myhierarchicalitem>


Bind the selected item in the treeview to the model. Also bind the label in the other panel to the same property. Now if you select an item in the treeview, the label for that item will automatically be bound in the other panel.

For a bit more information on databinding I suggest you take a look at the following video: http://channel9.msdn.com/posts/AdamKinney/Data-Binding-in-Windows-Presentation-Foundation/[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900