0

I've been tasked with making a few changes to an existing program.

One of those changes is implementing a 'save' button. When clicked, it will iterate through each tab and save the contents to a database, but I can't figure out how to access the data properly.

The tabs being added are separate views, each with their own viewmodel - the main view containing the tabcontrol also has its own viewmodel.

How would I go about accessing the tabcontrol, iterating through each tab and saving the data in an orderly fashion? (At this point I'm not sure if it's relevant to show any code, but please do request whatever you'd need)

4

2 に答える 2

0

ビューモデルですべてのデータにアクセスできることを正しく理解していれば、タブコントロールのタブを反復処理する必要はありません。その「保存」ボタンは、各タブのビューモデルからデータを収集するコマンドにバインドする必要があります。

于 2013-09-23T08:10:32.990 に答える
0

すべてのタブが変更の保存をサポートしていると仮定して、タブ ビュー モデルの上にあるビュー モデルを作成します。

// this is the base class for tab view models
public class DocumentViewModel
{
    public void SaveChanges() {}
}

// this is the view model for tab container
public class EditorViewModel
{
    private SaveChanges()
    {
        foreach (var document in OpenedDocuments)
        {
            document.SaveChanges();
        }        
    }

    public EditorViewModel()
    {
        SaveCommand = new RelayCommand(SaveChanges);
    }

    // this is your tabs
    public ObservableCollection<DocumentViewModel> OpenedDocuments { get; private set; }

    public ICommand SaveChangesCommand { get; private set; }
}
于 2013-09-23T07:18:37.870 に答える