ObservableCollection not noticing when Item in it changes (even with INotifyPropertyChanged)

前端 未结 18 2680
一生所求
一生所求 2020-11-22 02:06

Does anyone know why this code doesn\'t work:

public class CollectionViewModel : ViewModelBase {  
    public ObservableCollection Con         


        
18条回答
  •  别跟我提以往
    2020-11-22 02:49

    Instead of an ObservableCollection or TrulyObservableCollection, consider using a BindingList and calling the ResetBindings method.

    For example:

    private BindingList _tfsFiles;
    
    public BindingList TfsFiles
    {
        get { return _tfsFiles; }
        set
        {
            _tfsFiles = value;
            NotifyPropertyChanged();
        }
    }
    

    Given an event, such as a click your code would look like this:

    foreach (var file in TfsFiles)
    {
        SelectedFile = file;
        file.Name = "Different Text";
        TfsFiles.ResetBindings();
    }
    

    My model looked like this:

    namespace Models
    {
        public class TfsFile 
        {
            public string ImagePath { get; set; }
    
            public string FullPath { get; set; }
    
            public string Name { get; set; }
    
            public string Text { get; set; }
    
        }
    }
    

提交回复
热议问题