Answer the question
In order to leave comments, you need to log in
How to force ListBox to update with INotifyPropertyChanged?
class MyClassA
{
private ObservableCollection<AnotherClass> _listCode;
public ObservableCollection<AnotherClass> ListCode
{
get
{
return _listCode;
}
set
{
if (value == _listCode) return;
_listCode= value;
OnPropertyChanged("ListCode");
}
}
public void GetList()
{
ListCode = ...
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
<ListBox ItemsSource="{Binding Path=ListCode}" ItemTemplateSelector="{StaticResource ListTemplateSelector}" />
Answer the question
In order to leave comments, you need to log in
Option 1: Your class must inherit INotifyPropertyChanged, only then it will work.
Option 2: ObservableCollection implements INotifyPropertyChanged itself, so to use it your code should look something like this:
class MyClassA
{
public ObservableCollection<AnotherClass> ListCode { get; private set; };
public MyClassA()
{
// ...
ListCode = new ObservableCollection<AnotherClass>();
}
public void GetList()
{
ListCode.Clear();
// ...
ListCode.Add(new AnotherClass());
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question