我的TreeView不会在将元素添加到数据源时进行更新。我在xaml中使用此代码
这是代码隐藏:
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));
}
}
}
在codebehind中的addFunction
TreeNodeItem newSite= new TreeNodeItem{ Id = "ID", Desc = "Description" };
CustomerSiteTreeDataSource.Add(newSite);
我的TreeView在加载时正确绑定但不更新在向addfunction中的数据源添加新Item时的UI。
我做错了什么?
答案 0 :(得分:0)
您没有正确执行绑定。
卸下:
TV_CustomerSites.DataContext = CustomerSiteTreeDataSource;
添加:
Binding binding = new Binding("CustomerSiteTreeDataSource");
TV_CustomerSites.DataContext = this; // This might not be needed
TV_CustomerSites.SetBinding(TreeView.ItemsSourceProperty, binding);