【问题标题】:Tree View doesnot updates on adding object to the Observable collection树视图不会在将对象添加到 Observablecollection 时更新
【发布时间】:2014-02-02 18:42:35
【问题描述】:

将元素添加到数据源时,我的 TreeView 不会更新。我在 xaml 中使用此代码

这是代码隐藏:

公共部分类ProceduresPage:页面,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));
        }
    }


 } 

在代码隐藏的addFunction中

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

我的 TreeView 在加载时正确绑定,但不会更新在 addfunction 中向数据源添加新项目时的 UI。

我做错了什么?

【问题讨论】:

    标签: wpf treeview observablecollection


    【解决方案1】:

    你没有正确绑定。

    删除:

    TV_CustomerSites.DataContext = CustomerSiteTreeDataSource;
    

    添加:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-23
      • 1970-01-01
      • 2021-07-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多