gpt4 book ai didi

windows-phone-7 - 使用 MVVM 从 ListBox 中删除项目

转载 作者:行者123 更新时间:2023-12-03 10:18:10 25 4
gpt4 key购买 nike

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

这是 View 模型。我正在使用 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 绑定(bind)到项目列表的代码:
<ListBox Grid.Row="1" DataContext="{Binding Path=Measures}" ItemsSource="{Binding}" />

这是 ViewModel 到主容器的绑定(bind)
<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); }
}
}

最佳答案

我看不出有什么明显的错误。我只能建议尝试简化您的 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 发生了一些事情。 .

关于windows-phone-7 - 使用 MVVM 从 ListBox 中删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11168607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com