如何使用 mvvmlight 再次刷新绑定

How to refresh the binding again using mvvmlight

由于双向绑定,每当我更改其值时,CurrentSelectedBall 都会更新。
当我单击更新时,函数 UpdateRedBall 被调用,因此数据库中的红球被更新。但是球的视图列表没有更新,因为 ObservableCollection<RedBall> RedBalls 根本没有改变。
如何解决这个问题?我想 _context.SaveChanges();
之后需要做点什么 此外,我不能简单地执行 DataGridA.itemsSource = RedBalls 来进行硬刷新,因为第一个 DataGridA 在 MainviewModel 中不可访问。

部分方法:

public ObservableCollection<RedBall> RedBalls
{
    get { return new ObservableCollection<RedBall>(_context.RedBalls.OrderBy(m=>m.DateNumber));}
    set
    {
        _redBalls = value;
        RaisePropertyChanged("RedBalls");
    }
}
public RedBall CurrentSelectedRedBall
{
    get { return _currentSelectedRedBall; }
    set
    {
        _currentSelectedRedBall = value;
        RaisePropertyChanged("CurrentSelectedRedBall");
    }
}
private void UpdateRedBall()
{
    if (CurrentSelectedRedBall != null)
    {
        var ballToUpdate = _context.RedBalls.SingleOrDefault(x => x.Id == CurrentSelectedRedBall.Id);
        ballToUpdate.DateNumber = CurrentSelectedRedBall.DateNumber;
        ballToUpdate.First = CurrentSelectedRedBall.First;
        ballToUpdate.Second = CurrentSelectedRedBall.Second;
        ballToUpdate.Third = CurrentSelectedRedBall.Third;
        ballToUpdate.Fourth = CurrentSelectedRedBall.Fourth;
        ballToUpdate.Fifth = CurrentSelectedRedBall.Fifth;
        ballToUpdate.Sixth = CurrentSelectedRedBall.Sixth;
    }
    _context.SaveChanges();
    //RedBalls = RedBalls

}

使用类似这样的东西:https://gist.github.com/itajaja/7507120 - 基本上您需要订阅 PropertyChanged 并从中引发 CollectionChanged 事件。

我非常不喜欢 getter 'do stuff'。一个get应该是不可能失败的。我会考虑将您的 RedBalls getter 中的逻辑移到别处。我还担心您在每次获取时都会更新 RedBalls ObservableCollection,因为它可能会导致绑定问题。绑定的 ObservableCollections 应该实例化一次,然后使用添加、删除和清除进行更改。以这种方式与他们合作也应该解决适当更新 collection 的问题。

public ObservableCollection<RedBall> RedBalls
{
    get { return _redBalls; }
    set
    {
        _redBalls = value;
        RaisePropertyChanged("RedBalls");
    }
}

public RedBall CurrentSelectedRedBall
{
    get { return _currentSelectedRedBall; }
    set
    {
        _currentSelectedRedBall = value;
        RaisePropertyChanged("CurrentSelectedRedBall");
    }
}

private void UpdateCurrentSelectedRedBall()
{
    UpdateRedBall();

    var redBalls = _context.RedBalls.OrderBy(m=>m.DateNumber);
    if(redBalls != null)
    {
        RedBalls.Clear();
        foreach(RedBall rb in redBalls)
        {
            RedBalls.Add(rb);
        }
    }
}

private void UpdateRedBall()
{
    if (CurrentSelectedRedBall != null)
    {
        var ballToUpdate = _context.RedBalls.SingleOrDefault(x => x.Id == CurrentSelectedRedBall.Id);
        ballToUpdate.DateNumber = CurrentSelectedRedBall.DateNumber;
        ballToUpdate.First = CurrentSelectedRedBall.First;
        ballToUpdate.Second = CurrentSelectedRedBall.Second;
        ballToUpdate.Third = CurrentSelectedRedBall.Third;
        ballToUpdate.Fourth = CurrentSelectedRedBall.Fourth;
        ballToUpdate.Fifth = CurrentSelectedRedBall.Fifth;
        ballToUpdate.Sixth = CurrentSelectedRedBall.Sixth;
    }
    _context.SaveChanges();
}