我正在开发一个应用程序,允许用户将他们公司的员工详细信息输入数据库。到目前为止,我正在试验 WPF 并尝试在使用 Entity Framework 的同时在我的应用程序中实现 MVVM。
我正在创建一个主从应用程序,并且一直在研究如何使用 MVVM 实现这一点,因为我对这一切都非常陌生。
我尝试过的一种方法是在我的 View-Model
中创建一个名为 SelectedEmployee
的属性,然后将其绑定(bind)到 List View
在我的 xaml 中,像这样;
public Employee _SelectedEmployee;
public Employee SelectedEmployee
{
get
{
return _SelectedEmployee;
}
set
{
if (_SelectedEmployee == value)
return;
_SelectedEmployee = value;
OnPropertyChanged("SelectedEmployee");
}
}
<ListView HorizontalAlignment="Left" Name="listview" VerticalAlignment="Bottom" ScrollViewer.HorizontalScrollBarVisibility="Visible"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding LoadEmployee}" SelectionMode="Single" SelectedItem="{Binding SelectedEmployee, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="150" Grid.Row="1">
然后我有一个方法允许用户更新 List View
中的 SelectedItem
。但这就是问题发生的地方。当我从 List View
中选择一个项目时,它只会更新数据库中的第一行,而不是我想要选择的行。
这是方法;
public void UpdateEmployee(Employee emp)
{
using (DBEntities context = new DBEntities())
{
emp = context.Employees.Where(e => e.EmployeeID == SelectedEmployee.EmployeeID).FirstOrDefault();
emp.Title = Title;
emp.FirstName = FirstName;
emp.Surname = Surname;
emp.Position = Position;
emp.DateOfBirth = DateOfBirth;
emp.Address = Address;
emp.Country = Country;
emp.Postcode = Postcode;
emp.PhoneNumber = PhoneNumber;
emp.MobileNumber = MobileNumber;
emp.FaxNumber = FaxNumber;
emp.Email = Email;
emp.NINumber = NINumber;
emp.ChargableResource = ChargableResource;
emp.ChargeOutRate = ChargeOutRate;
emp.TimeSheetRequired = TimeSheetRequired;
emp.WorkShift = WorkShift;
emp.BenefitsProvided = BenefitsProvided;
context.Employees.ApplyCurrentValues(emp);
context.SaveChanges();
}
}
我已将我的 view model
中的属性绑定(bind)到我的 xaml 中的 text-boxes
,然后实现 OnPropertyChanged
。
我还使用命令来限制 code-behind
的数量,因为它对可测试性和可维护性很重要。
这里是更新的命令方法;
private ICommand showUpdateCommand;
public ICommand ShowUpdateCommand
{
get
{
if (showUpdateCommand == null)
{
showUpdateCommand = new RelayCommand(this.UpdateFormExecute, this.UpdateFormCanExecute);
}
return showUpdateCommand;
}
}
private bool UpdateFormCanExecute()
{
return !string.IsNullOrEmpty(FirstName) ...
}
private void UpdateFormExecute()
{
UpdateOrganisationTypeDetail();
}
由于我是 MVVM 的新手,我不太确定我做错了什么,所以请提供一些意见:)。
最佳答案
那么问题可能出在您的更新上。我真的不明白你想用 ListView 做什么,但由于它不能在简单的数据网格中工作,这可能会有所帮助。这与其说是一个答案,不如说是我为找到你的错误而写的东西,但恰好不包含你的错误。如果有任何问题,请发表评论。
public partial class MainWindow : Window
{
private static MainViewModel _mainViewModel = new MainViewModel();
public static ObservableCollection<Employee> staff = new ObservableCollection<Employee>();
public MainWindow()
{
InitializeComponent();
this.DataContext = _mainViewModel;
dataGrid1.DataContext = staff;
listview.DataContext = staff;
Employee Employee1 = new Employee();
Employee1.name = "Jeff";
staff.Add(Employee1);
Employee Employee2 = new Employee();
Employee2.name = "Jefferson";
staff.Add(Employee2);
Employee2.name = "Tim";
}
}
public class Employee
{
public string name { get; set; }
}
public class MainViewModel : INotifyPropertyChanged
{
private string _SelectedEmployee;
public string SelectedEmployee
{
get { return _SelectedEmployee; }
set
{
_SelectedEmployee = value;
NotifyPropertyChanged("SelectedEmployee");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
关于c# - SelectedItem 仅更新 WPF 数据库中的第一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13911590/