why does not working the following code?
I have this class:
public class ExportSetting : INotifyPropertyChanged
{
public Guid Guid { get; set; }
public string Name { get; set; }
private bool export;
public bool Export
{
get { return export; }
set
{
export = value;
NotifyPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged([CallerMemberName]string propertyName = null)
{
if (!string.IsNullOrEmpty(propertyName) && PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
And I created collection by using command yield:
public IEnumerable<ExportSetting> SequencesToExport
{
get { return sequencesToExport; }
set { sequencesToExport = value; }
}
...
sequencesToExport = FillSequencesSetting(sequences);
private IEnumerable<ExportSetting> FillSequencesSetting(List<MTFSequence> sequences)
{
foreach (var item in sequences)
{
yield return new ExportSetting(item.Id, item.Name, true);
}
}
When the property Export (from class ExportSetting) is changed from UI should be raised the event PropertyChanged but this event is null.
(ListBox in XAML has ItemsSource binding to property SequenceToExport)
When I modified creation of collection like this everything is working properly:
private IEnumerable<ExportSetting> FillSequencesSetting(List<MTFSequence> sequences)
{
List<ExportSetting> tmp = new List<ExportSetting>();
foreach (var item in sequences)
{
tmp.Add(new ExportSetting(item.Id, item.Name, true));
}
return tmp;
}
Why the command yield causes the event PropertyChanged isn't registered and is null and when i create collection by using the generic List the event is working properly? Thanks for your answers.