开发者

ObservableCollection in C#

开发者 https://www.devze.com 2023-02-13 14:00 出处:网络
Consider I have two classes. Professor and TimePerDay. public class TimePerDay { private ObservableCollection<TimeSpan> _subjTime;

Consider I have two classes.

Professor and TimePerDay.

 public class TimePerDay
{
    private ObservableCollection<TimeSpan> _subjTime;
    public ObservableCollection<TimeSpan> subjTime
    {
        get { return _subjTime; }
        set { _subjTime = value; } 
    }
}
private ObservableCollection<TimePerDay> _actualTime;
    public ObservableCollection<TimePerDay> actualTime
    {
        get { return _actualTime; }
        set { _actualTime = value; RaisePropertyChanged("actualTime"); }
    }

I'm trying to populate the TimePerDay class with each actualTime, my code is

                    ObservableCollection<TimePerDay> facultyTime = new ObservableCollection<TimePerDay>();
                    ObservableCollection<TimeSpan> ts = new ObservableCollection<TimeSpan>();
                    ts.Add(TimeSpan.FromHours(9));
                    facultyTime.Add(new TimePerDay() { subjTime = TimeSpan.FromHours(2) }); 

and it's giving me error tha开发者_运维百科t An instance of type System.TypeSpan can not be assigned to a member of type System.Collections.ObjectModel.ObservableCollection. How come it cannot assign a member while what I'm adding has a same type?


In this line:

facultyTime.Add(new TimePerDay() { subjTime = TimeSpan.FromHours(2) });

You are trying to create a new TimePerDay but are assigning a TimeSpan to subjTime which is an ObservableCollection<TimeSpan>. I suspect you want

facultyTime.Add(new TimePerDay() {
    subjTime = new ObservableCollection<TimeSpan> { TimeSpan.FromHours(2) }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消