使用簡單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();
}
現在
我的詳細信息子視圖我有這個Constractor
public EmployeeDetailView(EmployeeDetailViewModel viewModel)
{
InitializeComponent();
DataContext = viewModel;
}
,這裏是我的DetailsViewModel constractor
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"/>
等等..問題是,只有部門組合框正在更新源值,當我更改它的值
和其他組合框不..即使綁定語句完全相同!
很抱歉寫這麼多..但任何人都可以幫助我嗎?
非常感謝
對不起,人也沒有工作.. 奇怪的是,部門combobox是唯一一個來回流動數據,即使代碼是完全相同的所有他們 其餘的只是在子窗口關閉後將所有的值設置爲空! – 3oon
你可以給我最好的方式來處理這種情況嗎? – 3oon