MVVM List and ObservableCollection

本文关键字:ObservableCollection and List MVVM | 更新日期: 2023-09-27 18:02:34

所以我正在尝试使用WPF和MVVM light构建一个小的食谱应用程序。我遇到了将List从模型绑定到视图模型的情况。它可以很好地显示和删除项目,但当添加项目时,我无法让显示更新。

我遇到了ObserableCollections,这似乎正是我想要的,但我不确定我是否正确使用它们,因为每次创建一个新的OC似乎是错误的。当模型使用列表时,我应该如何检索可观察集合?

模型(s):

public class Recipe
{
    public int Id { get; set; }
    public string Title { get; set; }
    public List<RecipeIngredient> Ingredients { get; set; }
}
public class RecipeIngredient
{
    // ... //
}

ViewModel:

 public Recipe SelectedRecipe
{
    get
    {
        return this.selectedRecipe;
    }
    set
    {
        this.selectedRecipe = value;
        RaisePropertyChanged("SelectedRecipe");
        RaisePropertyChanged("RecipeIngredients");
    }
}
public ObservableCollection<RecipeIngredient> RecipeIngredients
{
    get 
    {
        return new ObservableCollection<RecipeIngredient>(selectedRecipe.Ingredients.ToList());
    }
}
public RelayCommand<EventArgs> AddIngredientCommand { get; private set; }
public RelayCommand<string> DeleteIngredientCommand { get; private set; }
private void AddIngredient(EventArgs eventArgs)
{
    SelectedRecipe.Ingredients.Add(new RecipeIngredient() { Name = "New Ingredient" }); 
    RaisePropertyChanged("RecipeIngredients");
}
private void DeleteIngredient(string name)
{
    SelectedRecipe.Ingredients = SelectedRecipe.Ingredients.Where(i => i.Name != name).ToList();
    RaisePropertyChanged("RecipeIngredients");
}
public MainViewModel()
{
    DBController db = new DBController();
    recipes = db.GetRecipeList();
    RecipeSelectionChangedCommand = new RelayCommand<SelectionChangedEventArgs>((args) => RecipeSelectionChanged(args));
    SaveRecipeCommand = new RelayCommand<EventArgs>((args) => SaveRecipe(args));    
    AddIngredientCommand = new RelayCommand<EventArgs>((args) => AddIngredient(args));
    DeleteIngredientCommand = new RelayCommand<string>((args) => DeleteIngredient(args));
}

我是不是离题太远了?

MVVM List and ObservableCollection

应该更仔细地阅读。如果你在另一个视图中显示所选配方的成分,你应该在视图<ListBox ItemsSource="{Binding SelectedRecipe.Ingredients}"/>中使用数据绑定,你可以考虑为ORM使用linq到实体(实体框架).

public class RecipeVM
{
    public RecipeVM(Recipe r)
    {
        recipe = r;
    }
    Recipe recipe;
    public int Id 
    { 
        get
        {
            return recipe.Id;
        }
        set
        {
            PropertyChanged("Id");
            recipe.id = value;
        }
    }
    public string Title
    {
        get
        {
            return recipe.Title;
        }
        set
        {
            PropertyChanged("Title");
            recipe.Title = value;
        }
    }
    ObservableCollection<RecipeIngredient> ingredients;
    public ObservableCollection<RecipeIngredient> Ingredients 
    {
        get
        {
            if (ingredients == null)
                ingredients = new ObservableCollection<RecipeIngredient>(recipe.Ingredients);
            return ingredients;
        }
        set
        {
            PropertyChanged("Ingredients");
            ingredients = value;
        }
    }
}

如果你想让集合保持同步,你需要稍微修改一下。