在通过 EF 服务层持久化数据的 MVVM LOB 应用程序中,我有以下 WPF EmployeeView,它通过绑定(bind)到 EmployeeViewModel 中包含的 Employee Model 对象来显示 Employee 数据(Employee 对象填充在 Controller 类中并通过同一类持久化):
这是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>
}
和 View 模型
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
.
关于WPF-MVVM : Adding New Entity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22172974/