MVVM:删除一个CustomerViewModel,但是如何获取其中的Customer模型呢?

发布于 2024-08-30 23:58:12 字数 144 浏览 7 评论 0原文

我在组合框中有一个 CustomerViewModel 列表。我想要删除选定的 CustomerViewModel 以及包装在其中的 Customer 以将其从存储库中删除。

但是如何访问 CustomerViewModel 中的 Customer 模型呢?

I have a list of CustomerViewModels in a ComboBox. The selected CustomerViewModel I want to delete and also the Customer wrapped inside it to remove it from the repository.

But how can I access the Customer model inside the CustomerViewModel?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

残月升风 2024-09-06 23:58:12

只是一个建议,将您的 customerviewmodels 集合设为 CustomerViewModels 的 ObserableCollection。
这给你带来的是一个 CollectionChanged 事件,你可以用委托来监听集合的更改,即删除,这样你就可以相应地操作你的模型

http://msdn.microsoft.com/en-us/library/ms653375(VS.85).aspx

也许有什么喜欢

public class CustomersViewModel: ViewModelBase
{
    public ObservableCollection<CustomersViewModel> Customers { get; private set; }

    public CustomersViewModel()
    {
        Customers = new ObservableCollection<CustomersViewModel>(GetCustomers());
        Customers.CollectionChanged += 
            (sender, args) =>
                {
                    if (args.Action ==  NotifyCollectionChangedAction.Remove)
                    {
                        foreach (CustomerViewModel customerViewModel in args.NewItems)
                        {
                            DeleteCustomer(customerViewModel.Customer);
                        }
                    }
                };
    }

    private void DeleteCustomer(Customer customer)
    {
        // Call into your repo and delete the customer.
    }

    private List<CustomersViewModel> GetCustomers()
    { 
        // Call into your model and return customers.
    }

    ... ICommands ect... 

}

Just a suggestion, make your collection of customerviewmodels an ObserableCollection of CustomerViewModels.
what this buys you is a CollectionChanged Event that you could listen on with a delegate for changes to the collection ie deletion, so from there you could manipulate you model accordingly

http://msdn.microsoft.com/en-us/library/ms653375(VS.85).aspx

perhaps something like

public class CustomersViewModel: ViewModelBase
{
    public ObservableCollection<CustomersViewModel> Customers { get; private set; }

    public CustomersViewModel()
    {
        Customers = new ObservableCollection<CustomersViewModel>(GetCustomers());
        Customers.CollectionChanged += 
            (sender, args) =>
                {
                    if (args.Action ==  NotifyCollectionChangedAction.Remove)
                    {
                        foreach (CustomerViewModel customerViewModel in args.NewItems)
                        {
                            DeleteCustomer(customerViewModel.Customer);
                        }
                    }
                };
    }

    private void DeleteCustomer(Customer customer)
    {
        // Call into your repo and delete the customer.
    }

    private List<CustomersViewModel> GetCustomers()
    { 
        // Call into your model and return customers.
    }

    ... ICommands ect... 

}
寄离 2024-09-06 23:58:12

您可能已经可以访问 CustomerViewModel 中的 Customer(VieModel 需要公开 Customer 的属性,以便 View 可以对它们进行数据绑定;我通常通过直接公开 Customer 或其副本来完成此操作)。

关键是您不应该自己删除客户。这就是 ViewModel 的用途,公开删除关联的 CustomerICommand。根据您使用的 MVVM 框架,查看 DelegateCommand 或其他同等内容。

您的 CustomerViewModel 将有

public ICommand DeleteCommand { get; private set; }

一个 CommandTarget (可能是 Button)到此命令。执行该命令时,将运行 CustomerViewModel 的私有方法,您可以从那里删除 Customer ,而不会将删除机制暴露给代码的其他部分。例如:

public CustomerViewModel()
{
    this.DeleteCommand = new DelegateCommand(this.ExecuteDeleteCommand);
}

private void ExecuteDeleteCommand()
{
    // remove the Customer from the ObservableCollection of customers
    // and also delete it from the database, or do anything else you want
}

You might have already access to the Customer inside CustomerViewModel (the VieModel needs to expose the properties of the Customer so the View can databind on them; I usually do it by exposing the Customer or a copy of it directly).

The point is that you should not delete the Customer yourself. That's what the ViewModel is for, to expose an ICommand that deletes the associated Customer. Depending on which MVVM framework you are using, look into DelegateCommand or another equivalent.

Your CustomerViewModel would have a

public ICommand DeleteCommand { get; private set; }

and your View would bind a CommandTarget (probably a Button) to this command. When the command is executed a private method of CustomerViewModel will be run, and you can delete the Customer from there without exposing the deletion mechanism to other parts of the code. For example:

public CustomerViewModel()
{
    this.DeleteCommand = new DelegateCommand(this.ExecuteDeleteCommand);
}

private void ExecuteDeleteCommand()
{
    // remove the Customer from the ObservableCollection of customers
    // and also delete it from the database, or do anything else you want
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文