代码之家  ›  专栏  ›  技术社区  ›  DaveB

在Silverlight中使用IEditableObject

  •  1
  • DaveB  · 技术社区  · 15 年前

    我有一个实现IEditableObject接口的对象,该接口在绑定到Silverlight页的ViewModel上公开。

    如何/在何处调用beginedit、canceledit和endedit方法?如何仅将实现此接口的对象约束到我的页面?

    我没有使用DataGrid或DataForm控件。我使用Label、TextBox和DescriptionViewer控件显示要编辑的数据。

    1 回复  |  直到 15 年前
        1
  •  6
  •   Savvas Sopiadis    15 年前

    我知道这是一条旧线(但为了将来使用…)

    我这样做:

    每当当前项(例如CollectionViewSource)更改时,都会执行以下操作:

    void View_CurrentChanged(object sender, EventArgs e)
            {
                if (culturesView.Source != null)
                {
                    ((IEditableObject)SelectedRecord).BeginEdit();
                    RaisePropertyChanged("SelectedRecord");
    
                }
            }
    

    每当我想保存(当前项目)时,我都会这样做:

     private void Save()
    {
     ((IEditableObject)SelectedRecord).EndEdit();
    //do the actual saving to the dbms here ....
    
    }
    

    每当我想取消(当前更改)时,我都会这样做:

    private void Cancel()
    {            
    ((IEditableObject)SelectedRecord).CancelEdit();
                //allthough we have canceled the editing we have to re-enable the edit mode (because
                //the user may want to edit the selected record again)
                ((IEditableObject)SelectedRecord).BeginEdit();
    
    }
    

    希望以后能帮助别人!