如何读取所有文本框值(映射到数据库文件)并将它们存储在WPF-MVVM中的数据库中
我是WPF和MVVM的新手。经过漫长的发明,我知道了如何从数据库中检索数据并将其绑定到itemcontrol/listview/gridview 但我的问题是,我不知道如何读取一堆文本框值并将其存储为数据库中的新记录 这是我的示例代码 看法如何读取所有文本框值(映射到数据库文件)并将它们存储在WPF-MVVM中的数据库中,wpf,mvvm,data-binding,Wpf,Mvvm,Data Binding,我是WPF和MVVM的新手。经过漫长的发明,我知道了如何从数据库中检索数据并将其绑定到itemcontrol/listview/gridview 但我的问题是,我不知道如何读取一堆文本框值并将其存储为数据库中的新记录 这是我的示例代码 看法 我的cs文件是 public ObservableCollection<DataAccess.Employee> AllEmployees { get;
我的cs文件是
public ObservableCollection<DataAccess.Employee> AllEmployees
{
get;
private set;
}
public EmployeeListViewModel(EmployeeRepository employeeRepository)
{
if (employeeRepository == null)
{
throw new ArgumentNullException("employeeRepository");
}
_employeeRepository = employeeRepository;
this.AllEmployees = new ObservableCollection<DataAccess.Employee>
(_employeeRepository.ListAll());
}
公共可观察收集所有员工
{
得到;
私人设置;
}
公共EmployeeListViewModel(EmployeeRepository EmployeeRepository)
{
if(employeeRepository==null)
{
抛出新ArgumentNullException(“employeeRepository”);
}
_employeeRepository=employeeRepository;
this.AllEmployees=新的可观察集合
(_employeeRepository.ListAll());
}
现在,我如何通过读取这些文本框在数据库中存储新员工的名字、姓氏和年龄
如何为NewEmployeeCommand事件编写函数,以读取文本框(将文本框映射到数据库中相应的数据文件),并将数据存储在数据库中
非常感谢 您可以在命令参数中传递引用:
<StackPanel>
<TextBox x:Name="FirstName"/>
<TextBox x:Name="LastName"/>
<Button Content="New" Command="{Binding NewEmployeeCommand}">
<Button.CommandParameter>
<x:Array Type="{x:Type TextBox}">
<x:Reference Name="FirstName"/>
<x:Reference Name="LastName"/>
</x:Array>
</Button.CommandParameter>
</Button>
</StackPanel>
通过绑定:
<Button.CommandParameter>
<local:MyEntry FirstName="{Binding ElementName=FirstName, Path=Text}"
LastName="{Binding ElementName=LastName, Path=Text}"/>
</Button.CommandParameter>
您无法读取文本框的值。您需要一个新的EmployeeViewModel并将文本框绑定到属性 编辑: 只需创建一个具有INotifyPropertyChanged和所需属性的类
public class NewEmployee : INotifyPropertyChanged
{
public string FirstName
{
get{return this._firstname;}
set{this._firstname = value;
OnPropertyChanged("FirstName");}
}
//... other properties
}
xaml
如果您试图使用MVVM,只需:
- 创建ViewModel以包含视图所需的所有属性
- 绑定到Xaml中的那些属性
public class EmployeeListViewModel
{
public ObservableCollection<Employee> AllEmployees {get;private set;}
public string FirstName {get;set;}
public string LastName {get;set;}
public int? Age {get;set;}
public ICommand NewEmployeeCommand {get;set;}
//You need to connect to this method by using a Delegate/RelayCommand see link below
public void AddNewEmployee()
{
//Add your real code here to actually insert into the db
var result = InsertEmployeeIntoDatabase(FirstName,LastName,Age);
//You probably want to add this new employee to the list now ;)
AllEmployees.Add(result);
//Now you probably want to reset your fields
FirstName = null;
LastName = null;
Age = null;
}
}
公共类EmployeeListViewModel
{
公共可观察集合AllEmployees{get;private set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共整数{get;set;}
公共ICommand NewEmployeeCommand{get;set;}
//您需要使用委托/中继命令连接到此方法,请参阅下面的链接
public void AddNewEmployee()
{
//在这里添加真正的代码以实际插入数据库
var result=InsertEmployeeIntoDatabase(FirstName、LastName、Age);
//您可能希望立即将此新员工添加到列表中;)
AllEmployees.Add(结果);
//现在您可能需要重置字段
FirstName=null;
LastName=null;
年龄=零;
}
}
然后像这样编辑您的xaml:
<ItemsControl ItemsSource="{Binding AllEmployees}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Width="100" Text="{Binding FirstName}" Margin="4"/>
<TextBox Width="100" Text="{Binding LastName}" Margin="4"/>
<TextBox Width="100" Text="{Binding Age}" Margin="4"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<!-- For new Employee Details -->
<StackPanel>
<TextBox Text={Binding FirstName}"/>
<TextBox Text={Binding LastName}"/>
<TextBox Text={Binding Age}"/>
<Button Content="New" Command="{Binding NewEmployeeCommand}"/>
</StackPanel>
我得到了正确的答案
我在xmal的新报名表应该是
<StackPanel>
<TextBox Text={Binding Employee.FirstName}"/>
<TextBox Text={Binding Employee.LastName}"/>
<TextBox Text={Binding Employee.Age}"/>
<Button Content="New" Command="{Binding NewEmployeeCommand}"/>
</StackPanel>
谢谢你的回答。但这会是一种MVVM模式吗?我不知道,对不起,我最近才开始研究MVVM。您可以在CommandParameter中创建一个对象并绑定它的值,我认为这样会更干净。我添加了绑定选项(可能与blindmeis所指的类似),但这需要依赖属性。@blindmeis:您能给我一些代码来做吗?感谢you@Blindmeis:谢谢你,伙计。但是我怎样才能把这些文本框映射到我的员工数据库呢?我不知道你的DAL,但是如果你有办法插入新行的话。只要满足条件。您没有将文本框映射到数据库。文本框绑定到您的虚拟机,您的虚拟机拥有您需要的所有数据。你用的是NHibernate还是其他ORM?很抱歉有点耽搁。。我正在使用Linq to Sql我不知道Linq to Sql,但我认为也有实体?也许你有一个叫做EmployeeEntity的实体。如果是这样,只需将属性数据映射到实体,并在新命令中调用entity.Save()。如果Linq to Sql实体实现INotifyProepertyChanged,则可以将该实体直接绑定到视图。顺便说一句,何塞张贴一个完整的样本
<StackPanel DataContext={Binding MyNewEmployeeProperty}>
<TextBox x:Name="FirstName" Text={Binding FirstName}/>
<TextBox x:Name="LastName" Text={Binding LastName}/>
<TextBox x:Name="Age" Text={Binding Age}/>
<Button Content="New" Command="{Binding NewEmployeeCommand}"/>
</StackPanel>
public class EmployeeListViewModel
{
public ObservableCollection<Employee> AllEmployees {get;private set;}
public string FirstName {get;set;}
public string LastName {get;set;}
public int? Age {get;set;}
public ICommand NewEmployeeCommand {get;set;}
//You need to connect to this method by using a Delegate/RelayCommand see link below
public void AddNewEmployee()
{
//Add your real code here to actually insert into the db
var result = InsertEmployeeIntoDatabase(FirstName,LastName,Age);
//You probably want to add this new employee to the list now ;)
AllEmployees.Add(result);
//Now you probably want to reset your fields
FirstName = null;
LastName = null;
Age = null;
}
}
<ItemsControl ItemsSource="{Binding AllEmployees}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Width="100" Text="{Binding FirstName}" Margin="4"/>
<TextBox Width="100" Text="{Binding LastName}" Margin="4"/>
<TextBox Width="100" Text="{Binding Age}" Margin="4"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<!-- For new Employee Details -->
<StackPanel>
<TextBox Text={Binding FirstName}"/>
<TextBox Text={Binding LastName}"/>
<TextBox Text={Binding Age}"/>
<Button Content="New" Command="{Binding NewEmployeeCommand}"/>
</StackPanel>
<StackPanel>
<TextBox Text={Binding Employee.FirstName}"/>
<TextBox Text={Binding Employee.LastName}"/>
<TextBox Text={Binding Employee.Age}"/>
<Button Content="New" Command="{Binding NewEmployeeCommand}"/>
</StackPanel>
public class EmployeeListViewModel : INotifyPropertyChanged
{
Employee _employee;
RelayCommand _addNewEmployee;
EmployeeRepository _employeeRepository;
public Employee Employee
{
get
{
return _employee;
}
set
{
_employee = value;
OnPropertyChanged("Employee");
}
}
public void NewEmployeeCommand()
{
if(_addNewEmployee == null)
{
_addNewEmployee = new RelayCommand( param => NewEmployeeCommandExecute(),
param => NewEmployeeCommandCanExecute
);
}
}
void NewEmployeeCommandExecute()
{
_employeeRepository.Add(Employee);
_employeeRepository.Save();
}
bool NewEmployeeCommandCanExecute
{
get
{
return true;
}
}
}