【问题标题】:Self-Deleting Viewmodel in detail view - Bad Practise?详细视图中的自删除视图模型 - 不好的做法?
【发布时间】:2013-03-16 20:20:09
【问题描述】:

我有一个主从应用程序,其中详细视图模型/视图能够执行删除命令。

但是我怎样才能通知主视图模型中的主集合细节视图模型已被删除并且必须从集合中删除?

这是一个糟糕的设计,主视图模型必须删除细节吗? 或者是通过事件做到这一点的唯一选择?符合 MVVM 吗?

这里是缩短的代码

视图模型

public class AllMetalTransactionViewModel : WorkspaceViewModel
{
    private ObservableCollection<MetalTransactionViewModel> _metalTransactions;
    public ObservableCollection<MetalTransactionViewModel> MetalTransactions
    {
        get { return _metalTransactions; }
        set
        {
            if (Set("MetalTransactions", ref _metalTransactions, value))
            {

            }
        }
    }

    private MetalTransactionViewModel _selectedMetalTransaction;
    public MetalTransactionViewModel SelectedMetalTransaction
    {
        get { return _selectedMetalTransaction; }
        set
        {
            if (Set("SelectedMetalTransaction", ref _selectedMetalTransaction, value))
            {

            }
        }
    }
}

public class MetalTransactionViewModel : WorkspaceViewModel
{
    private RelayCommand _deleteCommand;
    public RelayCommand DeleteCommand
    {
        get
        {
            return _deleteCommand
                   ?? (_deleteCommand = new RelayCommand(
                        () =>
                            {
                                if (!IsNewUnit)
                                {
                                    _dataService.DeleteMetalTransaction(_metalTransaction, CallbackDelete);
                                    _dataService.CommitAllChanges(delegate(bool b, object o) {  });

                                    // How can I inform the AllMetalTransactionViewModel that I'm deleted? Event?
                                }
                            },
                        () => !IsNewUnit));
        }
    }
}

XAML 大师

<View:MetalTransactionView Grid.Column="1" 
DataContext="{Binding SelectedMetalTransaction}"></View:MetalTransactionView>

XAML 详细信息

<Button DockPanel.Dock="Right" HorizontalAlignment="Right" 
Padding="5" Content="Löschen" Margin="5" Width="80" 
Command="{Binding Path=DeleteCommand}" />

【问题讨论】:

    标签: c# .net xaml mvvm viewmodel


    【解决方案1】:

    祝你有美好的一天!

    您可以通过多种方式做到这一点(我喜欢 A 和 D 解决方案):

    A. 详细视图模型具有指向主详细视图模型(使用一种方法void RemoveDetail(MetalTransactionViewModel detail) 的一些轻量级接口)或详细视图模型集合的链接。 例如(有收藏链接):

    细节视图模型:

    public class MetalTransactionViewModel : WorkspaceViewModel
    {
        private RelayCommand _deleteCommand;
    
        IList<MetalTransactionViewModel> ParentCollection { get; }
    
    public RelayCommand DeleteCommand
        {
            get
            {
                return _deleteCommand
                       ?? (_deleteCommand = new RelayCommand(
                            () =>
                                {
                                    if (!IsNewUnit)
                                    {
                                        _dataService.DeleteMetalTransaction(_metalTransaction, CallbackDelete);
                                        _dataService.CommitAllChanges(delegate(bool b, object o) {  });
    
                                         if (ParentCollection == null) { return; }
    if (ParentCollection.Contains(this)) { ParentCollection.Remove(this); }
                                    }
                                },
                            () => !IsNewUnit));
            }
        }
    
    }
    

    在主视图模型中,创建详细视图模型时:

    private MetalTransactionViewModel CreateDetailViewModel()
    {
      return new MetalTransactionViewModel() { ParentCollection = MetalTransactions };
    }
    

    B.按你说的使用事件(但要小心,因为它可能会给你带来内存泄漏)。请查看WeakEventManager

    C.如果您使用 mvvm 工具包,例如 MVVM Light Toolkit,您可以使用 Messenger 类通知主视图模型删除操作。

    D.将删除命令移动到主视图模型。我想在这种情况下这是最好的解决方案。我相信主视图模型必须操纵集合详细信息视图模型

    希望对你有帮助!

    【讨论】:

    • 不错的答案丹尼尔,投票! @masterchris_99 我会选择选项 D,毕竟 MasterVM 应该知道它的细节/孩子(或奴隶:-P)
    猜你喜欢
    • 1970-01-01
    • 2020-12-13
    • 2022-08-08
    • 2020-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    • 2013-08-10
    相关资源
    最近更新 更多