从属性集中访问ObservableCollection项

时间:2015-08-19 12:55:53

标签: c# wpf

给定以下用作WPF DataContext的类:

class ViewModel
{
    public ObservableCollection<Task> Tasks { get; set; }
}

和这个Task类:

public class Task
{
    private string startTime;

    public Task ID { get; set; }
    public string StartTime
    {
        get { return startTime; }
        set
        {
            // Access ObservableCollection items
            startTime = value;
            OnPropertyChanged("StartTime");
        }
    }
}

我怎样才能在代码中看到&#34; //访问ObservableCollection项&#34;,访问Tasks ObservableCollection中的其他项,以便我可以将正在设置的实例的StartTime与Tasks ObservableCollection中的其他任务项的StartTime?

1 个答案:

答案 0 :(得分:-1)

您有两种选择:

  1. 您可以将ViewModel注入任务

    public class Task
    {
        private ViewModel viewModel;
        public class Task(ViewModel viewModel)
        {
            this.viewModel = viewModel;
        }
        ...
    }
    

    但是,这使得Task类只能用于ViewModel类,因为它们是松散耦合的。

  2. 您可以在ViewModel中执行StartTime验证。为此,您需要捕获所有任务的PropertyChanged事件。

    我创建的ExtendedObservableCollection会在任何项目的属性发生变化时触发事件:

    public class ExtendedObservableCollection<T> : ObservableCollection<T> where T : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler ItemPropertyChanged;
        protected override void ClearItems()
        {
            foreach (var item in Items) item.PropertyChanged -= OnItemPropertyChanged;
            base.ClearItems();
        }
    
        protected override void InsertItem(int index, T item)
        {
            item.PropertyChanged += OnItemPropertyChanged;
            base.InsertItem(index, item);
        }
    
        protected override void RemoveItem(int index)
        {
            this[index].PropertyChanged -= OnItemPropertyChanged;
            base.RemoveItem(index);
        }
    
        protected override void SetItem(int index, T item)
        {
            this[index].PropertyChanged -= OnItemPropertyChanged;
            item.PropertyChanged += OnItemPropertyChanged;
            base.SetItem(index, item);
        }
    
        protected virtual void OnItemPropertyChanged(object sender, PropertyChangedEventArgs e)
       {
           var handler = ItemPropertyChanged;
           if (handler != null) handler(sender, e);
       }
    }
    

    最后验证或者你需要什么

    public class ViewModel
    {
       public ExtendedObservableCollection<Task> Tasks { get; set; }
       public ViewModel()
       {
           Tasks=new ExtendedObservableCollection<Task>();
           Tasks.ItemPropertyChanged += TaskPropertyChanged;
       }
    
       private void TaskPropertyChanged(object sender, PropertyChangedEventArgs e)
       {
           var changedTask = (Task)sender;
           if (e.PropertyName == "StartTime")
           {
              if (!IsStartTimeGreatedThenPrevious(changedTask )) 
                  changedTask.SetError("StartTime", "Start time has to be greated than in previous task)
           }
       }        
    }
    
  3. 您可以在任务集合中添加或删除项目,ExtendedObservableCollection负责附加/分离到PropertyChanged事件。