MVVM List和ObservableCollection

时间:2014-05-09 04:42:37

标签: c# wpf mvvm mvvm-light observablecollection

所以我试图使用WPF和MVVM光来构建一个小的cookbook应用程序。我遇到了将List从模型绑定到视图模型的情况。它适用于显示和删除项目,但在添加项目时,我无法让显示更新。

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

模型(或多个):

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

public class RecipeIngredient
{
    // ... //
}

视图模型:

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

我离开这里了吗?

1 个答案:

答案 0 :(得分:4)

应该仔细阅读。如果您在备用视图中显示所选配方的成分,则应在视图中使用数据绑定<ListBox ItemsSource="{Binding SelectedRecipe.Ingredients}"/>您可以考虑将ORQ实体(实体框架)用于ORM ..

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

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