简体   繁体   中英

ObservableCollection<T>(List<T>) Constructor not working

I am not really sure if I am missing something very basic here or if there's a problem with this constructor.

public class SingleQuestion : INPC
{
    private String _questionText;
    private EDifficulty _difficulty;
    private ObservableCollection<String> _answerList;
    private ObservableCollection<Image> _imageList;

    public String QuestionText
    {
        get
        {
            return _questionText;
        }
        set
        {
            if (String.IsNullOrEmpty(value) || value == _questionText)
                return;

            _questionText = value;
            OnPropertyChanged();
        }
    }

    public EDifficulty Difficulty
    {
        get
        {
            return _difficulty;
        }
        set
        {
            if (_difficulty == value)
                return;

            _difficulty = value;
            OnPropertyChanged();
        }
    }

    public ObservableCollection<String> AnswerList
    {
        get
        {
            return _answerList;
        }
        set
        {
            if (value == null)
                return;

            _answerList = value;
            OnPropertyChanged();
        }
    }

    public ObservableCollection<Image> ImageList
    {
        get
        {
            return _imageList;
        }
        set
        {
            if (_imageList == null)
                return;

            _imageList = value;
            OnPropertyChanged();
        }
    }

    public SingleQuestion(String questionText, EDifficulty difficulty, List<String> answers, List<Image> images)
    {
        QuestionText = questionText;
        Difficulty = difficulty;
        AnswerList = new ObservableCollection<string>(answers);
        ImageList = new ObservableCollection<Image>(images);
    }
}

ImageList is always null and I don't know why. images is at least an empty list or has items in it, so ImageList should be initialized with an empty list at least.

Image class:

public class Image : INPC
{
    private String _imagePath;
    private bool _overwrite;

    public String ImagePath
    {
        get { return _imagePath; }
        set
        {
            _imagePath = value;
            OnPropertyChanged();
        }
    }

    public bool Overwrite
    {
        get { return _overwrite; }
        set
        {
            _overwrite = value;
            OnPropertyChanged();
        }
    }

    public Image(String imgPath, bool overwrite)
    {
        ImagePath = imgPath;
        Overwrite = overwrite;
    }
}

I didn't find any references to this problem, can someone give me any hints?

UPDATE:

I added a testcase. As you can see the constructor works as expected at the test variable, but does not within the constructor of the SingleQuestion class. I also included the complete SingleQuestion class.

在此处输入图片说明

Isn't it because of the setter?

_imageList is always null upon initialization. So, the setter simply returns.

 public ObservableCollection<Image> ImageList
    {
        get
        {
            return _imageList;
        }
        set
        {
////THIS CHECK
            if (_imageList == null)
                return;


            _imageList = value;
            OnPropertyChanged();
        }
    }

You could just set it directly in the setter:

  set
   {
    _imageList = value
    OnPropertyChanged();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM