0

我有一个显示ObservableCollection<Worker>. 双击单元格时,会打开一个新窗口,其中显示有关所选单元格的数据Worker并允许用户修改某些属性。在这个子窗口中有“接受”和“取消”按钮。Worker我的意图是在单击“接受”或“取消”之前进行任何临时更改。如果单击“接受”,我希望将这些更改永久化,如果单击“取消”,则将其丢弃。我还没有找到一种方法来做到这一点。激活后ExecuteHireCommand,数据立即发送回主项目。我需要一种仅在单击“接受”时才使更改永久生效的方法。您能提供的任何建议将不胜感激。

public class WorkerWindowViewModel : ViewModelBase
{
    #region Members

    private Worker _worker;

    #endregion

    #region Properties

    public string WindowKey { get; set; }

    public Worker Worker
    {
        get { return _worker; }
        set
        {
            if (_worker == value)
                return;
            _worker = value;
            RaisePropertyChanged(() => Worker);
        }
    }

    #endregion

    #region Construction

    public WorkerWindowViewModel()
    {
        CreateCommands();
    }

    #endregion

    #region Methods

    private void CreateCommands()
    {
        HireCommand = new RelayCommand(() => ExecuteHireCommand());

        AcceptCommand = new RelayCommand(() => ExecuteAcceptCommand());
        CancelCommand = new RelayCommand(() => ExecuteCancelCommand());
    }

    #endregion

    #region Commands

    public RelayCommand HireCommand { get; private set; }
    public RelayCommand AcceptCommand { get; private set; }
    public RelayCommand CancelCommand { get; private set; }

    private void ExecuteHireCommand()
    {
        Worker.IsEmployed = true;
        Worker.Employer = DataManager.Data.MasterGuildList[0];
    }
    private void ExecuteAcceptCommand()
    {
        Messenger.Default.Send<Worker>(Worker);

        Messenger.Default.Send<CloseWindowMessage>(new CloseWindowMessage(), WindowKey);
    }

    private void ExecuteCancelCommand()
    {
        Messenger.Default.Send<CloseWindowMessage>(new CloseWindowMessage(), WindowKey);
    }

    #endregion
}

如果有帮助,这里是创建子窗口的方式。

Messenger.Default.Register<OpenWorkerWindowMessage>(this, message =>
        {
            var uniqueKey = System.Guid.NewGuid().ToString();
            var workerWindowVM = SimpleIoc.Default.GetInstance<WorkerWindowViewModel>(uniqueKey);
            workerWindowVM.Worker = message.Argument;
            workerWindowVM.WindowKey = uniqueKey;
            var workerWindow = new WorkerWindowView(uniqueKey)
            {
                DataContext = workerWindowVM,
                Owner = this
            };
            workerWindow.Closed += (sender, args) => SimpleIoc.Default.Unregister(uniqueKey);
            workerWindow.Show();
        });
4

1 回答 1

0

打开以编辑所选工作人员的窗口需要对其自己的工作人员数据副本进行操作,以便在/除非单击接受之前不会修改可观察集合的内容 - 接受操作将更新所选工作人员从“编辑工作者”窗口操作的“临时”副本中收集。

于 2013-06-17T23:33:47.917 回答