简体   繁体   English

如何使用Caliburn.Micro MVVM在WPF中的同一视图上从另一个视图模型更新list <>?

[英]How to update list<> from another View model on same View in WPF using Caliburn.Micro MVVM?

I'm new to WPF and I want to follow MVVM framework using Caliburn Micro. 我是WPF的新手,我想使用Caliburn Micro遵循MVVM框架。 I have got stuck update a list in a view model from another one. 我不得不从另一个视图视图模型中更新列表。

I have 3 Views: 我有3个意见:

  • POSView : Contains two Content Controls for 2 other Views POSView:包含两个其他视图的两个内容控件
  • ProductView : List of all products ProductView:所有产品列表
  • CartView : List of all products added in cart CartView:购物车中添加的所有产品列表

On click of Product in Product View, product should get added in Cart View 在“产品视图”中单击“产品”后,应在“购物车视图”中添加产品

POSViewModel.cs 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: On AddProdClick(ProductModel productModel), I want to add clicked product to CartView. 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)
        {

        }

    }

ProductView.xaml: its a user control having product list. ProductView.xaml:具有产品列表的用户控件。 Button with product is bind to AddProdClick in View Model. 产品按钮绑定到视图模型中的AddProdClick。

 <Button Content="Add To Cart" cal:Message.Attach="AddProdClick($datacontext)" />

CartViewModel.cs CartViewModel.cs

public class CartViewModel : Conductor<object>
    {
        private BindableCollection<ProductModel> _CartProducts;
        public BindableCollection<ProductModel> CartProducts
        {
            get
            {
                return _CartProducts;
            }
            set
            {
                NotifyOfPropertyChange(() => CartProducts);
                _CartProducts = value;
            }
        }

        public CartViewModel()
        {
            CartProducts = new BindableCollection<ProductModel>();
        }
    }

I expect to Add item to Cart. 我希望将商品添加到购物车。

you could use CartViewModel as Args: 您可以将CartViewModel用作Args:

    public POSViewModel()
    {   
        CartViewModel = new CartViewModel();    
        ProductsViewModel = new ProductsViewModel(CartViewModel);
    }

and use it in constuctor of ProductsViewModel 并在ProductsViewModel的构造函数中使用它

public class ProductsViewModel : Conductor<object>
{
    public BindableCollection<ProductModel> Products { get; set; }
    public CartViewModel CVM { get; set; }
    public ProductsViewModel(CartViewModel CVM)
    {
                    this.CVM = CVM;
    }

    public void AddProdClick(ProductModel productModel)
    {
                    CVM.Add(productModel)
    }
}

you have another solution : using the PosViewModel: 您还有另一种解决方案:使用PosViewModel:

 public POSViewModel()
{   
    CartViewModel = new CartViewModel();    
    ProductsViewModel = new ProductsViewModel(this);
}

public class ProductsViewModel : Conductor<object>
{
    public BindableCollection<ProductModel> Products { get; set; }
    public CartViewModel CVM { get; set; }
    public ProductsViewModel(POSViewModel PVM)
    {
                    this.CVM = PVM.CartViewModel;
    }

    public void AddProdClick(ProductModel productModel)
    {
                    CVM.Add(productModel)
    }
}

A third solution is to use EventAggregator you need to modify some coding 第三种解决方案是使用EventAggregator,您需要修改一些编码

see EventAggregator 请参阅EventAggregator

When you click, you do EventAggregator.publish(new Addevent) in the Add method 单击时,在Add方法中执行EventAggregator.publish(new Addevent)

and in PosviewModel you catch the event... 然后在PosviewModel中捕获事件...

but for that you have to modify some lines of code, but read the link its not complex 但是为此,您必须修改一些代码行,但要阅读链接并不复杂

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM