c# - MVVM使用命令混淆将更改保存到 View 模型

标签 c# wpf entity-framework mvvm

我有一个显示GridViewObservableCollection<Models>。当我选择一个项目时,TextBoxe被填充以允许编辑和添加新的Model。但是,当我编辑文本框时,gridview和ViewModel会自动使用修订版本进行更新。我正尝试使用按钮命令来保存/取消修订。

我包括了 View ViewModel 。我在这方面还相对较新,而且我一直在关注本教程http://msdn.microsoft.com/en-us/library/ff798384.aspx,但我不确定我可以进行哪些更改以使其按我的方式工作。

当我创建一个NewModel时,SelectedModel也随NewModel一起变化。如此迷茫!

我的看法:

<ListView ItemsSource="{Binding Models}" SelectedItem="{Binding SelectedModel}" 
          IsSynchronizedWithCurrentItem="True">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Models" DisplayMemberBinding="{Binding ModelName}">
            <GridViewColumn Header="Template" 
                            DisplayMemberBinding="{Binding Template.TemplateID}">
        </GridView>
    </ListView.View>
</ListView>
    <TextBox DataContext="{Binding SelectedModel}" Text="{Binding ModelName, Mode=TwoWay}"/>
    <TextBox DataContext="{Binding SelectedModel}" Text="{Binding TemplateID, Mode=TwoWay}"/>
    <Button Content="Save" Command="{Binding SaveModelCommand}" />
    <Button Content="Cancel" Command="{Binding CancelModelCommand}" />
    <Button Content="Add Model" Command="{Binding AddModelCommand}" />

我的ViewModel:
private Model _selectedModel = null;
private Model _newModel = null;
private RelayCommand _addmodelcommand;
private RelayCommand _editmodelcommand;
private RelayCommand _savemodelcommand;

public Model SelectedModel
{
    get { return _selectedModel; }
    set
    {
        if (_selectedModel != value)
        {
            _selectedModel = value; RaisePropertyChanged("SelectedModel");
        }
    }
}

public Model NewModel
{
    get { return _newModel; }
    set 
    {
       _newModel = value;
       RaisePropertyChanged("NewModel");
    }
}

public RelayCommand AddModelCommand
{
    get
    {
        if (_addmodelcommand == null)
        {
           _addmodelcommand = new RelayCommand(p => SetNewModel(),
                                               p => true);
        }
        return _addmodelcommand;
    }
    set 
    { 
        _addmodelcommand = value;
        RaisePropertyChanged("AddModel");
    }
}

public RelayCommand SaveModelCommand
{
    get
    {
        if (_savemodelcommand == null)
        {
            _savemodelcommand = new RelayCommand(p => ModelSaved(), 
                                                 p => true);
        }
        return _savemodelcommand;
    }
    set
    {
        _savemodelcommand = value;
        RaisePropertyChanged("SaveModel");
    }
}

public void SetNewModel()
{
    if (NewModel == null)
    { 
       NewModel = new Model();
    }
    else
    {
       NewModel.ModelName = string.Empty; 
    }
    SelectedModel = NewModel;
}

public string ModelSaved()
{
    string error = null;
    if (error == null)
    {
        if (SelectedModel != NewModel)
        {
            UpdateModel(SelectedModel);
        }
        else //adds new model
        {
          //Add the new model to the data context.
          _ESTContext.Models.Add(NewModel);

          //Add the new model to the observable collection.
          this.Models.Add(NewModel);

          this.SelectedModel = NewModel;
          NewModel = null;
        }
        _ESTContext.SaveChanges();
    }
    return error;
}

最佳答案

我注意到您正在使用EntityFramework,它旨在跟踪您的更改并仅在您调用_ESTContext.SaveChanges()时保存它们。因此,我将更改您处理此问题的方式。

当前,似乎要让用户单击“保存”或“取消”,然后再移至ListView中的另一个项目。我建议您让用户单击一个项目,使用您的文本框对其进行修改,如果需要,请单击另一个项目并对其进行修改。用户还可以根据需要多次单击添加按钮。然后,在完成所有更改后,单击“保存”按钮应仅对所有数据进行任何错误检查(如果需要),然后调用_ESTContext.SaveChanges()。这将立即保存所有更改。同样,将取消按钮设为全局取消按钮,可以像执行_ESTContext = new WhateverContext();然后刷新Models属性一样简单地实现。

这种方法还允许用户在需要时单击“保存”按钮,因此理论上如果他们确实愿意,在每次修改后仍可以保存。

关于c# - MVVM使用命令混淆将更改保存到 View 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25433223/

相关文章:

c# - LINQ to SQL where 子句返回零

c# - 如何强制 FlowDocument 刷新其在 DataContext 集上的内容?

entity-framework - 为什么 EF5 代码在将可为空的日期时间插入数据库时​​首先使用 datetime2?

c# - Entity Framework - 使用外键获取空异常

C# 多个 If 语句替换

c# - 如何复制 C# 3D 锯齿状数组

c# - DocumentDB 替换文档失败

c# - 自动滚动到 WPF 文本 block 的末尾

c# - WPF Glyph Run 错误地渲染度数符号

c# - 对象必须实现 IConvertible