0
votes

My TreeView doesnot updates on adding the element to datasource. I am using this code in xaml

This is the codebehind:

public partial class ProceduresPage : Page, INotifyPropertyChanged {

 public ProceduresPage()
    {

            InitializeComponent();
            CustomerSiteTreeDataSource = new ObservableCollection<TreeNodeItem>();
            CustomerSiteTreeDataSource.Add(TreeNodeItem newSite= new TreeNodeItem{  Id=     "ID", Desc = "Description" });
            TV_CustomerSites.DataContext = CustomerSiteTreeDataSource;

     }



    private ObservableCollection<TreeNodeItem> customerSiteTreeDataSource;
    public ObservableCollection<TreeNodeItem> CustomerSiteTreeDataSource
    {
        get
        {
            return customerSiteTreeDataSource;
        }
        set
        {

            customerSiteTreeDataSource = value;
            NotifyPropertyChanged("CustomerSiteTreeDataSource");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }


 } 

In the addFunction in codebehind

TreeNodeItem newSite= new TreeNodeItem{  Id = "ID", Desc = "Description" };  
CustomerSiteTreeDataSource.Add(newSite);

My TreeView binds correctly on load but doesn't updates The UI on adding a new Item to the datasource in the addfunction.

What am I doing wrong?

1

1 Answers

0
votes

You didn't do the binding correctly.

Remove:

TV_CustomerSites.DataContext = CustomerSiteTreeDataSource;

Add:

Binding binding = new Binding("CustomerSiteTreeDataSource");
TV_CustomerSites.DataContext = this;  // This might not be needed
TV_CustomerSites.SetBinding(TreeView.ItemsSourceProperty, binding);