那怎么办
SourceUpdated
事件?
SourceUpdated事件没有启动,但我尝试了ObservableCollection.CollectionChanged 它是好的,所以我想请你给一个小代码,这是我测试过的,希望它有帮助:
public partial class Window1 : Window
{
ObservableCollection<string> items = new ObservableCollection<string>()
{
"string1","string2","string3","string4","string5"
};
public Window1()
{
InitializeComponent();
DataContext = this;
tree.ItemsSource = items;
items.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(items_CollectionChanged);
}
void items_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
MessageBox.Show("Event raised");
}
private void btnAddItem_Click(object sender, RoutedEventArgs e)
{
items.Add("string6");
}
}
还有xaml
<StackPanel Orientation="Vertical">
<TreeView x:Name="tree" />
<Button x:Name="btnAddItem" Click="btnAddItem_Click" Content="AddItem" />
</StackPanel>