如何使用mvvmlight再次刷新绑定

时间:2015-07-29 11:02:45

标签: wpf mvvm-light

enter image description here

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

一些方法:

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

}

2 个答案:

答案 0 :(得分:0)

使用类似这样的内容:https://gist.github.com/itajaja/7507120 - 基本上你需要订阅PropertyChanged并从中提出CollectionChanged事件。

答案 1 :(得分:0)

我非常不喜欢那些做事情的吸气者。失败应该是不可能的。我会考虑将RedBalls getter中的逻辑移到别处。我还担心你在每次获取时都会新建RedBalls ObservableCollection,因为它可能会导致绑定问题。 Bound ObservableCollections应该实例化一次,然后使用Add,Remove和Clear进行更改。以这种方式使用它们也应该解决适当更新集合的问题。

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();
}