我使用简单的 MVVM 框架来创建一个简单的 Silverlight 4.0 LOB 应用程序。
我有一个员工列表视图,其中显示了所有员工的列表,并且我的 EmployeeListViewModel 中有一些属性如下:
private Grade selectedGrade;
public Grade SelectedGrade
{
get { return selectedGrade; }
set
{
selectedGrade = value;
NotifyPropertyChanged(m => m.SelectedGrade);
}
}
private Religion selectedReligion;
public Religion SelectedReligion
{
get { return selectedReligion; }
set
{
selectedReligion = value;
NotifyPropertyChanged(m => m.SelectedReligion);
}
}
private ObservableCollection<Grade> grades;
public ObservableCollection<Grade> Grades
{
get { return grades; }
set
{
grades = value;
NotifyPropertyChanged(m => m.Grades);
}
}
private ObservableCollection<Religion> religions;
public ObservableCollection<Religion> Religions
{
get { return religions; }
set
{
religions = value;
NotifyPropertyChanged(m => m.Religions);
}
}
private ObservableCollection<Department> departments;
public ObservableCollection<Department> Departments
{
get { return departments; }
set
{
departments = value;
NotifyPropertyChanged(m => m.Departments);
}
}
private Employee selectedEmployee;
public Employee SelectedEmployee
{
get { return selectedEmployee; }
set
{
selectedEmployee = value;
SetCanProperties();
NotifyPropertyChanged(m => m.SelectedEmployee);
}
}
private ObservableCollection<Employee> employees;
public ObservableCollection<Employee> Employees
{
get { return employees; }
set
{
employees = value;
NotifyPropertyChanged(m => m.Employees);
}
}
private Department selectedDepartment;
public Department SelectedDepartment
{
get { return selectedDepartment; }
set
{
selectedDepartment = value;
NotifyPropertyChanged(m => m.SelectedDepartment);
}
}
现在在我看来,我有一个按钮可以在我的员工列表中编辑选定的员工,它会打开一个新的子窗口,其中包含要编辑的 EmployeeDetails
EmployeeListViewModel viewModel;
public EmployeeListView()
{
InitializeComponent();
viewModel = (EmployeeListViewModel)DataContext;
}
这是编辑员工方法
private void editItemButton_Click(object sender, RoutedEventArgs e)
{
// Exit if no product selected
if (viewModel.SelectedEmployee == null) return;
// Create a product detail model
EmployeeDetailViewModel detailModel =
new EmployeeDetailViewModel(viewModel.SelectedEmployee);
// set comboboxes !!
detailModel.Departments = viewModel.Departments;
detailModel.Religions = viewModel.Religions;
detailModel.Grades = viewModel.Grades;
// Start editing
detailModel.BeginEdit();
// Show EmployeeDetail view
EmployeeDetailView itemDetail = new EmployeeDetailView(detailModel);
itemDetail.Closed += (s, ea) =>
{
if (itemDetail.DialogResult == true)
{
// Confirm changes
detailModel.EndEdit();
}
else
{
// Reject changes
detailModel.CancelEdit();
}
};
itemDetail.Show();
}
现在在我的详细信息子视图中我有这个承包商
public EmployeeDetailView(EmployeeDetailViewModel viewModel)
{
InitializeComponent();
DataContext = viewModel;
}
这是我的 DetailsViewModel 构造器
public EmployeeDetailViewModel(Employee model)
{
base.Model = model;
}
private ObservableCollection<Religion> religions;
public ObservableCollection<Religion> Religions
{
get { return religions; }
set
{
religions = value;
NotifyPropertyChanged(m => m.Religions);
}
}
private ObservableCollection<Grade> grades;
public ObservableCollection<Grade> Grades
{
get { return grades; }
set
{
grades = value;
NotifyPropertyChanged(m => m.Grades);
}
}
private ObservableCollection<Department> departments;
public ObservableCollection<Department> Departments
{
get { return departments; }
set
{
departments = value;
NotifyPropertyChanged(m => m.Departments);
}
}
毕竟现在是绑定我有部门,宗教和等级的三个组合框(这是我的员工表中的外键)
<ComboBox ItemsSource="{Binding Departments}" DisplayMemberPath="DepartmentName" SelectedValue="{Binding Model.Emp_Department, Mode=TwoWay}" SelectedValuePath="DepartmentId"/>
<ComboBox ItemsSource="{Binding Grades}" DisplayMemberPath="GradeName" SelectedValue="{Binding Model.Emp_Grade, Mode=TwoWay}" SelectedValuePath="GradeId"/>
等等..问题是当我更改其值时,只有部门组合框正在更新源值
并且其他组合框不会..即使绑定语句完全相同!
很抱歉写了这么多..但是有人可以帮我吗?
多谢