我有一个显示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();
});