Search code examples
c#wpfdependency-propertiesitemssource

Force dependency property to refresh


I want to force the binding of my Listbox to refresh its content. My listbox is bounded to a dependency property:

<ListBox ... DataContext="{Binding ElementName=_this}" ItemsSource="{Binding Path=MyList}"/>

I want to refresh the listbox content when, for example, I press a button in such a way that the DependencyProperty MyList get is called.


Solution

  • You could make MyList an ObservableCollection, or something else which implements INotifyCollectionChanged

    Then if you changed the contents of MyList the ListBox would be updated automatically

    In this case you wouldn't even need to declare MyList as dependency property. A simple readonly property would be sufficient:

    public ObservableCollection<MyItem> MyList { get; }
        = new ObservableCollection<MyItem>();