使用MVVM从ListBox中删除项目

时间:2012-06-23 10:04:31

标签: windows-phone-7 mvvm mvvm-light windows-phone-7.1.1

我试图从ListBox中删除一个项目。该命令已正确触发,并且该项目已从数据库中正确删除,但该列表未刷新。

这是ViewModel。我使用的是MVVM Light 4.1

public class ViewAllViewModel : ViewModelBase
{
    public ViewAllViewModel()
    {
        NavigateToAddNew = new RelayCommand(() => NavigationController<Views>.Current.NavigateTo(Views.AddNew));
        Remove = new RelayCommand<int>(DeleteMeasure);

        using (var repository = App.ServiceLocator.Get<IRepository>())
        {
            Measures = new ObservableCollection<Measure>(repository.Measures);
        }
    }

    private void DeleteMeasure(int measureId)
    {
        Measure measure;

        using (IRepository repository = App.ServiceLocator.Get<IRepository>())
        {
            measure = repository.Measures.Single(m => m.Id == measureId);
            repository.Measures.Delete(measure);
            repository.SaveChanges();
        }

        measure = Measures.Single(m => m.Id == measureId);
        if (Measures.Remove(measure))
        {
            RaisePropertyChanged(() => Measures);
        }
    }

    public RelayCommand NavigateToAddNew { get; set; }
    public RelayCommand<int> Remove { get; set; }

    private ObservableCollection<Measure> _measures;
    public ObservableCollection<Measure> Measures
    {
        get { return _measures; }
        set { Set(() => Measures, ref _measures, value); }
    }
}

感谢您的帮助。

PS:我知道有类似的问题,但没有一个接受的答案对我有用:(

编辑1 这是我在XAML页面中使用的代码,用于将ListBox绑定到项目列表:

<ListBox Grid.Row="1" DataContext="{Binding Path=Measures}" ItemsSource="{Binding}" />

这里是ViewModel与主容器的绑定

<Grid DataContext="{Binding Source={StaticResource Locator}, Path=ViewAll}" x:Name="LayoutRoot" />

编辑2 这是ViewModel的完整代码

public class ViewAllViewModel : ViewModelBase
{
    public ViewAllViewModel()
    {
        NavigateToAddNew = new RelayCommand(() => NavigationController<Views>.Current.NavigateTo(Views.AddNew));
        Remove = new RelayCommand<int>(DeleteMeasure);

        LoadMeasures();

        Messenger.Default.Register<PropertyChangedMessage<ObservableCollection<Measure>>>(this, message => LoadMeasures());
    }

    private void LoadMeasures()
    {
        using (var repository = App.ServiceLocator.Get<IRepository>())
        {
            Measures = new ObservableCollection<Measure>(repository.Measures.OrderByDescending(m => m.MeasureDate).ThenByDescending(m => m.Id).Take(20));
        }
    }

    private void DeleteMeasure(int measureId)
    {
        Measure measure;

        using (IRepository repository = App.ServiceLocator.Get<IRepository>())
        {
            measure = repository.Measures.Single(m => m.Id == measureId);
            repository.Measures.Delete(measure);
            repository.SaveChanges();
        }

        measure = Measures.Single(m => m.Id == measureId);
        Measures.Remove(measure);

        RaisePropertyChanged("LastMeasure", null, measure, true);
    }

    public RelayCommand NavigateToAddNew { get; set; }
    public RelayCommand<int> Remove { get; set; }

    private ObservableCollection<Measure> _measures;
    public ObservableCollection<Measure> Measures
    {
        get { return _measures; }
        set { Set(() => Measures, ref _measures, value); }
    }
}

1 个答案:

答案 0 :(得分:1)

我没有看到任何明显的错误。我建议的是尝试将ListBox简化为这样:

<ListBox Grid.Row="1" ItemsSource="{Binding Path=Measures}" />

删除调用RaisePropertyChanged(() => Measures);的代码(因为不需要它)。

如果这些都不起作用,我会测试看看如果你完全重置Measures属性会发生什么,如:

private void DeleteMeasure(int measureId)
{
    using (IRepository repository = App.ServiceLocator.Get<IRepository>())
    {
        var measure = repository.Measures.Single(m => m.Id == measureId);
        repository.Measures.Delete(measure);
        repository.SaveChanges();
    }

    Measures = repository.Measures;
}

如果这导致成功刷新ListBox,则意味着ObservableCollection会发生某些事情。