【问题标题】:WPF-MVVM: Adding New EntityWPF-MVVM:添加新实体
【发布时间】:2014-03-04 13:14:28
【问题描述】:

在通过 EF 服务层持久化数据的 MVVM LOB 应用程序中,我有以下 WPF EmployeeView,它通过绑定到包含在 EmployeeViewModel 中的 Employee Model 对象来显示 Employee 数据(Employee 对象填充在控制器类中并通过同一类持久化) :

这里是 xaml:

{
<TextBlock Grid.Column="0" Grid.Row="0" Text="ID:" />
        <TextBox Grid.Column="1" Grid.Row="0" 
                 Text="{Binding Employee.ID,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="50" />

        <TextBlock Grid.Column="0" Grid.Row="1" Text="First Name:" />
        <TextBox Grid.Column="1" Grid.Row="1" 
                 Text="{Binding Employee.FirstName,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="150" />

        <TextBlock Grid.Column="0" Grid.Row="2" Text="Last Name:" />
        <TextBox Grid.Column="1" Grid.Row="2" 
                 Text="{Binding Employee.LastName,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="150" />


        <TextBlock Grid.Column="0" Grid.Row="4" Text="Department:" />
        <ComboBox Grid.Column="1" Grid.Row="4" Width="150" SelectedValuePath="." 
                  SelectedItem="{Binding Employee.Department,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                  ItemsSource="{Binding Employee.DepartmentLookup}" DisplayMemberPath="DepartmentName" />

        <TextBlock Grid.Column="0" Grid.Row="6" Text="Birth Date:" />
        <DatePicker Grid.Column="1" Grid.Row="6" 
                    SelectedDate="{Binding Employee.BirthDate,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

        <TextBlock Grid.Column="0" Grid.Row="7" Text="Hire Date:" />
        <DatePicker Grid.Column="1" Grid.Row="7" 
                    SelectedDate="{Binding Employee.HireDate,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

        <Button  Height="25" Width="50" Grid.Column="1" Grid.Row="8" Command="{Binding Path=NewEmpCommand}">
                <TextBlock Margin="1">New</TextBlock>
        </Button>

        <Button Margin="150,5,5,5" Height="25" Width="50" Grid.Column="1" Grid.Row="8" 
                Command="{Binding Path=AddEmpCommand}">
                <TextBlock Margin="1">Add</TextBlock>
        </Button>
}

和视图模型

    public class EmployeeViewModel : INotifyPropertyChanged
{


    //NewEmpCommand
    private DelegateCommand _newEmpCommand;
    public DelegateCommand NewEmpCommand
    {
        get
        {
            return _newEmpCommand??
                   (_newEmpCommand= new DelegateCommand(NewEmployee));
        }
    }

    //AddEmpCommand
    private DelegateCommand _addEmpCommand;
    public DelegateCommand AddEmpCommand
    {
        get
        {
            return _addEmpCommand??
                   (_addEmpCommand= new DelegateCommand(AddEmployee));
        }
    }


    //ctor-------------------------------------------------------------------------

    public EmployeeViewModel ()
    {
        Initialize();
    }

    private void Initialize()
    {
     //
    }

    private void NewEmployee()
    {
        this.Employee = null;
    }


    //-------------------------------------------------------------------------
    void AddEmployee()
    {
      //here I send the Employee Model object to the service layer to be persisted 
    }


    //Binding----------------------------------------------------------------------

    private Employee _employee;
    public Employee Employee        
    {
        get { return _employee; }
        set
        {
            if (_employee!= value)
            {
                _employee= value;
                NotifyPropertyChanged("Employee");
            }

        }
    }



   //---------------------------------------------------------
    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}

我省略了不必要的细节。现在我想点击新建按钮清除所有控件,所以我使用语句“this.Employee = null;”(如果不正确,请纠正我) 然后我想将新值输入到控件中(据我所知)它应该分配给 Employee 模型对象。

问题:

1.这是清除准备输入新数据的控件的正确方法

2.当我输入新数据并尝试保存时(通过将 Employee 对象发送到 EF 服务层以保存更改,Employee 对象为 null 并且过程失败。如何解决此问题?

【问题讨论】:

  • 想一想,在单击“新建”按钮时创建全新的Employee 对象怎么样?这将回答这两个问题。而且逻辑正确,点击“新建”按钮->创建“新建”Employee
  • @har07 新的Employee对象如何从控件中获取数据
  • @codemania,请注意,编辑旨在改善帖子,而不仅仅是为了给用户声誉。我注意到您最近花时间编辑了很多帖子。还请花时间正确标记您的编辑...您可以将它们标记为所有代码应该像,无论如何您的编辑实际涉及的内容。
  • @Sheridan.. 感谢您的建议。我会处理这些事情
  • @Hussein 自动?如果您正确设置,数据绑定将处理它(您发布的代码 sn-p 对我来说看起来不错)。试试看。

标签: wpf entity-framework mvvm


【解决方案1】:

根据要求将我的评论转换为回答:

只需单击“新建”按钮即可创建一个全新的 Employee 对象。这将回答这两个问题:

  1. 这是一种更好的清除方式(如果我不能声称它是正确的方式) 控制准备输入新数据
  2. 这样可以避免Employee对象在尝试保存时为null的问题

而且逻辑正确,点击“新建”按钮->创建“新建”Employee

【讨论】:

    猜你喜欢
    • 2010-11-10
    • 1970-01-01
    • 1970-01-01
    • 2012-09-02
    • 1970-01-01
    • 1970-01-01
    • 2011-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多