I have the following custom canvas that fires when the children are added or deleted.
public class CustomCanvas : Canvas, INotifyPropertyChanged
{
protected override void OnVisualChildrenChanged(DependencyObject visualAdded, DependencyObject visualRemoved)
{
OnPropertyChanged(new PropertyChangedEventArgs(""));
}
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
PropertyChanged(this, e);
}
public event PropertyChangedEventHandler PropertyChanged;
}
I also have a Listbox where i wish to display the canvas's children's names.
public void Button_Click_1(object sender, RoutedEventArgs e)
{
shapes= new ObservableCollection<string>();
foreach (FrameworkElement drawing in customCanvas.Children)
{
shapes.Add(drawing.Name);
}
listBox.ItemsSource = shapes;
}
The problem I have is, the listbox doesn't update when I add any Framework Element to my Custom Canvas. It requires me clicking Button_1. I thought ObservableCollection and InotifyPropertyChanged would automatically update my listbox. Please help.