IEditableObject in MVVM

2019-02-02 17:43发布

Can you think of a scenario where IEditableObject would be still usefull in an MVVM-based WPF application? If so, do you have an example that demonstrates this.

3条回答
Melony?
2楼-- · 2019-02-02 17:54

I have used IEditableObject in one of my applications. For example if you have a dialog for editing something, you could implement IEditableObject on your ViewModel. You call BeginEdit() when the dialog opens, EndEdit() when the user clicks OK, and CancelEdit() when the user clicks cancel.

IEditableObject is a good interface anytime you want to be able to roll back changes.

查看更多
迷人小祖宗
3楼-- · 2019-02-02 17:58

I've got an implementation of IEditableObject in my application so that I can keep from updating my data model if what the user has entered is invalid, and roll back to the original values if the user abandons changes.

查看更多
叼着烟拽天下
4楼-- · 2019-02-02 18:03

Within a type being displayed in a DataGrid. This was needed since when I am making use of tabs and a DataGrid is being stored within that tab switching the tabs needed to force a commit so to speak within the DataGrid if a cell was active; rolling back the changes since they were not committed. T

There is a behavior being applied to the DataGrid to achieve this functionality but the IEditableObject portion is below.

private IDatabaseConnection _copy;

void IEditableObject.BeginEdit()
{
    if (this._copy == null)
        this._copy = _container.Resolve<IDatabaseConnection>();

    _copy.Database = this.Database;
    _copy.DisplayName = this.DisplayName;
    _copy.HostName = this.HostName;
    _copy.Username = this.Username;
    _copy.Password = this.Password;
}

void IEditableObject.CancelEdit()
{
    this.Database = _copy.Database;
    this.DisplayName = _copy.DisplayName;
    this.HostName = _copy.HostName;
    this.Username = _copy.Username;
    this.Password = _copy.Password;
}

void IEditableObject.EndEdit()
{
    _copy.Database = String.Empty;
    _copy.DisplayName = String.Empty;
    _copy.HostName = String.Empty;
    _copy.Username = String.Empty;
    _copy.Password = String.Empty;
}
查看更多
登录 后发表回答