item属性发生更改时,dataGrid不会更新
所以我有几乎与TextBoxes / ListBoxes等设置相同的东西,但它似乎并不适用于dataGrid ..
所以我有一个包含dataGrid的视图索引。
我创建了一个IndexModel类,如下所示:
public class IndexModel : INotifyPropertyChanged
{
    private ObservableCollection<Schedule> _schedules;
    public IndexModel(ObservableCollection<Schedule> schedules)
    {
        _schedules = schedules;
    }
    public ObservableCollection<Schedule> Schedules
    {
        get { return _schedules; }
        set
        {
            _schedules = value;
            OnPropertyChanged();
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
然后在我的IndexView中创建IndexModel。
    private ObservableCollection<Schedule> _schedules;
    public Index(MainController controller)
    {
        _controller = controller;
        InitializeComponent();
        _schedules = controller.DatabaseController.GetSchedules() as ObservableCollection<Schedule>;
        DataContext = new IndexModel(_schedules);
        Log.Info($"UI Component {componentName} loaded succesfully",componentName, Source);
    }
我创建DataContext并将其绑定到XAML中
ItemsSource="{Binding Path=Schedules, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" 
我甚至创建了一个简单的void。
Schedule selectedItem = (Schedule) dataGrid.SelectedItem;
selectedItem.Name = "Testing";
它更新ObservableCollection但dataGrid不更新...
我查看了所有stackoverflow的答案,但仍然无法解决我的问题..
public partial class Schedule
{
    public int Id { get; set; }
    [Required]
    [StringLength(50)]
    public string Name { get; set; }
    public DateTime DateFrom { get; set; }
    public DateTime DateTo { get; set; }
    public virtual User User { get; set; }
    [StringLength(256)]
    public string Comment { get; set; }
    [StringLength(256)]
    public string Description { get; set; }
    [Required]
    public Priorities Priority { get; set; }
    public virtual UpdaterObject Object { get; set; }
    public virtual ICollection<ScheduleAction> ScheduleAction { get; set; }
  ObservableCollection在添加或删除项目时引发通知:其存在的原因是实现INotifyCollectionChanged 。  当它自己的属性改变时它也会引发通知(它也实现INotifyPropertyChanged ,所以你可以绑定到obcoll.Count )。 
  你没有做那些;  实际上, ObservableCollection完全没有办法知道你做了什么。  它会如何发现?  谁会告诉它? 
  为了知道其中一个项目的属性已更改,该项目必须实施INotifyPropertyChanged并且该集合必须处理通知。  那么,一旦这个项目做到了这一点,就没有必要收集这个项目的通知了。 
具有更改通知的规则是通知由其个人状态已更改的对象引发。 如果成员或包含项目发生更改,则不涉及父/容器。 由于上面给出的理由,如果它试图去做就没有任何意义。
  因此: Schedule需要实现INotifyPropertyChanged并在PropertyChanged的Name属性值更改时引发PropertyChanged 。 
  上面的规则意味着如果你有一个绑定会触及的类,那么除非它是不可变的(比如String ),它需要实现INotifyPropertyChanged 。 
根据您的示例,如果您在集合中动态添加或删除计划,应该起什么作用。
但是,如果您想要更新UI中的给定时间表,请执行以下操作:
selectedItem.Name = "Testing";
你想要的是更新一个Shedule项目本身,而不是收集Schedule。
换句话说,如果您希望在视图中对其进行编辑,则需要视图模型作为Schedule。 你也需要为Shedule提供一个数据模板,让WPF知道它是如何渲染Shedule的。
希望能帮助到你。
链接地址: http://www.djcxy.com/p/75241.html