Skip to main content
 首页 » 编程设计

c#之MVVM 列表和 ObservableCollection

2024年02月13日11hnrainll

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

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

型号:

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

View 模型:

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

我是不是跑题了?

请您参考如下方法:

应该更仔细地阅读。如果您在替代 View 中显示所选食谱的成分,则应在 View 中使用数据绑定(bind) <ListBox ItemsSource="{Binding SelectedRecipe.Ingredients}"/>您可以考虑使用 linq to entities (Entity Framework) for 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; 
        } 
    } 
} 

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