猿问

如何使用 Caliburn.Micro MVVM 从 WPF 中同一视图上的另一个视图模型更新列表

我是 WPF 新手,我想使用 Caliburn Micro 遵循 MVVM 框架。我无法从另一个视图模型中更新视图模型中的列表。

我有3个观点:

  • POSView :包含另外 2 个视图的两个内容控件

  • 产品视图:所有产品的列表

  • CartView :购物车中添加的所有产品的列表

单击产品视图中的产品后,产品应添加到购物车视图中

POSViewModel.cs

public class POSViewModel : Conductor<object>.Collection.AllActive

    {

        #region Private Variables

        private ProductsViewModel _ProductsViewModel;

        private CartViewModel _CartViewModel;

        #endregion


        #region Public Variables

        public ProductsViewModel ProductsViewModel

        {

            get { return _ProductsViewModel; }

            set { _ProductsViewModel = value; }

        }


        public CartViewModel CartViewModel

        {

            get { return _CartViewModel; }

            set { _CartViewModel = value; }

        }

        #endregion


        #region Public Methods

        public POSViewModel()

        {

            ProductsViewModel = new ProductsViewModel();

            CartViewModel = new CartViewModel();

        }

        #endregion

    }

ProductsViewModel.cs:在 AddProdClick(ProductModel ProductModel) 上,我想将单击的产品添加到 CartView。


public class ProductsViewModel : Conductor<object>

    {

        public BindableCollection<ProductModel> Products { get; set; }

        public ProductsViewModel()

        {

            Products = new BindableCollection<ProductModel>();

            for (int i = 0; i < 25; i++)

            {

                Products.Add(new ProductModel

                {

                    ProductName = "Product" + i.ToString(),

                    Qty = i + 2,

                    Rate = i * 10

                }); ;

            }


        }


        public void AddProdClick(ProductModel productModel)

        {


        }


    }


我希望将商品添加到购物车。


萧十郎
浏览 91回答 1
1回答

婷婷同学_

您可以使用 CartViewModel 作为参数:&nbsp; &nbsp; public POSViewModel()&nbsp; &nbsp; {&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; CartViewModel = new CartViewModel();&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; ProductsViewModel = new ProductsViewModel(CartViewModel);&nbsp; &nbsp; }并在 ProductsViewModel 的构造函数中使用它public class ProductsViewModel : Conductor<object>{&nbsp; &nbsp; public BindableCollection<ProductModel> Products { get; set; }&nbsp; &nbsp; public CartViewModel CVM { get; set; }&nbsp; &nbsp; public ProductsViewModel(CartViewModel CVM)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.CVM = CVM;&nbsp; &nbsp; }&nbsp; &nbsp; public void AddProdClick(ProductModel productModel)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CVM.Add(productModel)&nbsp; &nbsp; }}您还有另一个解决方案:使用 PosViewModel:&nbsp;public POSViewModel(){&nbsp; &nbsp;&nbsp; &nbsp; CartViewModel = new CartViewModel();&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; ProductsViewModel = new ProductsViewModel(this);}public class ProductsViewModel : Conductor<object>{&nbsp; &nbsp; public BindableCollection<ProductModel> Products { get; set; }&nbsp; &nbsp; public CartViewModel CVM { get; set; }&nbsp; &nbsp; public ProductsViewModel(POSViewModel PVM)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.CVM = PVM.CartViewModel;&nbsp; &nbsp; }&nbsp; &nbsp; public void AddProdClick(ProductModel productModel)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CVM.Add(productModel)&nbsp; &nbsp; }}第三种解决方案是使用EventAggregator,您需要修改一些编码请参阅事件聚合器单击时,您在 Add 方法中执行 EventAggregator.publish(new Addevent)在 PosviewModel 中你可以捕捉到事件......但为此你必须修改一些代码行,但阅读链接并不复杂
随时随地看视频慕课网APP
我要回答