当前位置: 首页 > 知识库问答 >
问题:

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

柴兴贤
2023-03-14

我是 WPF 的新手,我想使用卡利本微遵循 MVVM 框架。我卡住了从另一个视图模型中更新列表的问题。

我有3个观点:

  • POSView:包含另外2个视图的两个内容控件
  • ProductView:所有产品的列表
  • 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
    }

产品视图模型。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)
        {

        }

    }

产品视图:它是一个具有产品列表的用户控件。包含产品的按钮绑定到“视图模型”中的“添加”单击。

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

购物车视图模型.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>();
        }
    }

我希望将项目添加到购物车。

共有1个答案

金昌胤
2023-03-14

您可以使用CartViewModel作为参数:

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

并在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)
    }
}

您有另一个解决方案:使用PosView模型:

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

第三种解决方案是使用EventAggregator,您需要修改一些代码

请参阅EventAggregator

单击时,将执行EventAggregator。在Add方法中发布(新Addevent)

在PosviewModel中,您可以捕捉事件…

但为此,您必须修改一些代码行,但读取链接并不复杂

 类似资料:
  • 本文向大家介绍wpf 视图模型,包括了wpf 视图模型的使用技巧和注意事项,需要的朋友参考一下 示例 视图模型是MV VM中的“ VM” 。这是一个充当中介的类,将暴露model(s)给用户界面(视图),并处理来自视图的请求,例如单击按钮引发的命令。这是一个基本的视图模型: 构造函数创建一个Customer模型对象,并将其分配给CustomerToEdit属性,以使视图可见。 构造函数还创建一个R

  • 问题内容: 搜索后,我没有在网上或任何其他资源中找到任何东西,我想知道您是否可以通过联接另一个视图和一些其他表来形成一个视图?我猜类似的东西与Server_ref.part_notification_view是要加入的视图。 问题答案: 您当然可以在另一个视图的基础上建立一个视图: 但是您不能引用基础表中的任何内容,包括不属于视图的任何字段: 具有相同列的基础表不是问题,并且如果您在视图中包含的内

  • 问题内容: 我的一个视图需要添加一个项目以及其他功能,但是我已经有另一个视图专门添加了一个项目。 我可以做类似的事情吗? 问题答案: View函数应将呈现的HTML返回给浏览器(在中)。在视图中调用视图意味着(可能)要进行两次渲染。相反,只需将“添加”分解为不是视图的另一个函数,并让两个视图都调用它。

  • 以下是活动2的代码:包helloworld.app; 以下是活动1的xml文件代码: 编辑:以下是来自logcat的错误消息 08-01 07:01:11.673:E/AndroidRuntime(1326):at Android.view.view$1.onclick(View.java:3578) 08-01 07:01:11.673:E/AndroidRuntime(1326):at And

  • 我试图完成的是将上下文菜单放置到在另一个视图中引用的列表框项中: 这可以很好地显示我的MyListBoxView.xaml。但是,我想知道的是,是否可以向视图添加上下文菜单:来自ListBoxPresenterView.xaml的MyListBoxView。原因是我只想让MyListBoxView.xaml尽可能通用。所以如果我想对它做任何特殊的修改,那么就让它只在引用它的类中。 所以本质上是这样

  • 我正在使用DropWizard和Freemarker构建一个视图,该视图基于Web服务的结果显示不同类型的表单。 我将表单创建为视图——每个视图都有自己的ftl。 因此,在我的资源中,我发现我需要哪种表单,然后加载main.ftl,将表单视图作为参数传递(见下文)。 这不起作用。有人能看出我们哪里出了问题吗?或者,是否有完全不同的方法使用DropWizard和freemarker将视图链接在一起?